2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 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.
|
|
|
|
*/
|
|
|
|
|
2015-09-23 00:52:57 +02:00
|
|
|
#define TRACE_TAG SOCKETS
|
2015-03-19 23:21:08 +01:00
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-11-08 03:51:54 +01:00
|
|
|
#include <algorithm>
|
2019-01-11 23:42:08 +01:00
|
|
|
#include <chrono>
|
2016-05-18 02:46:27 +02:00
|
|
|
#include <mutex>
|
2016-03-01 17:58:26 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-11-08 03:51:54 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#if !ADB_HOST
|
2016-09-24 00:40:03 +02:00
|
|
|
#include <android-base/properties.h>
|
2017-04-14 08:48:57 +02:00
|
|
|
#include <log/log_properties.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#endif
|
2015-03-19 23:21:08 +01:00
|
|
|
|
|
|
|
#include "adb.h"
|
|
|
|
#include "adb_io.h"
|
2018-12-19 22:37:41 +01:00
|
|
|
#include "adb_utils.h"
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "transport.h"
|
2018-03-08 01:52:28 +01:00
|
|
|
#include "types.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-01-11 23:42:08 +01:00
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2016-05-18 02:46:27 +02:00
|
|
|
static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
|
2009-03-04 04:32:55 +01:00
|
|
|
static unsigned local_socket_next_id = 1;
|
|
|
|
|
2018-01-31 22:15:51 +01:00
|
|
|
static auto& local_socket_list = *new std::vector<asocket*>();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
/* the the list of currently closing local sockets.
|
|
|
|
** these have no peer anymore, but still packets to
|
|
|
|
** write to their fd.
|
|
|
|
*/
|
2018-01-31 22:15:51 +01:00
|
|
|
static auto& local_socket_closing_list = *new std::vector<asocket*>();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2013-12-13 14:09:44 +01:00
|
|
|
// Parse the global list of sockets to find one with id |local_id|.
|
|
|
|
// If |peer_id| is not 0, also check that it is connected to a peer
|
|
|
|
// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
|
2016-05-18 01:55:06 +02:00
|
|
|
asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
|
2018-01-31 22:15:51 +01:00
|
|
|
asocket* result = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-05-18 02:46:27 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
|
2018-01-31 22:15:51 +01:00
|
|
|
for (asocket* s : local_socket_list) {
|
2016-05-18 01:55:06 +02:00
|
|
|
if (s->id != local_id) {
|
2013-12-13 14:09:44 +01:00
|
|
|
continue;
|
2016-05-18 01:55:06 +02:00
|
|
|
}
|
2013-12-13 14:09:44 +01:00
|
|
|
if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
|
2010-06-12 16:40:20 +02:00
|
|
|
result = s;
|
|
|
|
}
|
2013-12-13 14:09:44 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
void install_local_socket(asocket* s) {
|
2016-05-18 02:46:27 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
s->id = local_socket_next_id++;
|
2013-12-13 14:09:44 +01:00
|
|
|
|
|
|
|
// Socket ids should never be 0.
|
2016-05-18 01:55:06 +02:00
|
|
|
if (local_socket_next_id == 0) {
|
2018-10-19 22:59:44 +02:00
|
|
|
LOG(FATAL) << "local socket id overflow";
|
2016-05-18 01:55:06 +02:00
|
|
|
}
|
2013-12-13 14:09:44 +01:00
|
|
|
|
2018-01-31 22:15:51 +01:00
|
|
|
local_socket_list.push_back(s);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
void remove_socket(asocket* s) {
|
2017-09-13 20:17:33 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
|
2018-01-31 22:15:51 +01:00
|
|
|
for (auto list : { &local_socket_list, &local_socket_closing_list }) {
|
|
|
|
list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }),
|
|
|
|
list->end());
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
void close_all_sockets(atransport* t) {
|
|
|
|
/* this is a little gross, but since s->close() *will* modify
|
|
|
|
** the list out from under you, your options are limited.
|
|
|
|
*/
|
2016-05-18 02:46:27 +02:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
|
2009-03-04 04:32:55 +01:00
|
|
|
restart:
|
2018-01-31 22:15:51 +01:00
|
|
|
for (asocket* s : local_socket_list) {
|
2016-05-18 01:55:06 +02:00
|
|
|
if (s->transport == t || (s->peer && s->peer->transport == t)) {
|
2016-05-18 19:39:48 +02:00
|
|
|
s->close(s);
|
2009-03-04 04:32:55 +01:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 21:20:29 +01:00
|
|
|
enum class SocketFlushResult {
|
|
|
|
Destroyed,
|
|
|
|
TryAgain,
|
|
|
|
Completed,
|
|
|
|
};
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-03-19 21:20:29 +01:00
|
|
|
static SocketFlushResult local_socket_flush_incoming(asocket* s) {
|
2018-04-03 23:37:11 +02:00
|
|
|
if (!s->packet_queue.empty()) {
|
|
|
|
std::vector<adb_iovec> iov = s->packet_queue.iovecs();
|
|
|
|
ssize_t rc = adb_writev(s->fd, iov.data(), iov.size());
|
|
|
|
if (rc > 0 && static_cast<size_t>(rc) == s->packet_queue.size()) {
|
|
|
|
s->packet_queue.clear();
|
2018-03-19 21:20:29 +01:00
|
|
|
} else if (rc > 0) {
|
2018-04-03 23:37:11 +02:00
|
|
|
// TODO: Implement a faster drop_front?
|
|
|
|
s->packet_queue.take_front(rc);
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent_add(s->fde, FDE_WRITE);
|
2018-03-19 21:20:29 +01:00
|
|
|
return SocketFlushResult::TryAgain;
|
|
|
|
} else if (rc == -1 && errno == EAGAIN) {
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent_add(s->fde, FDE_WRITE);
|
2018-03-19 21:20:29 +01:00
|
|
|
return SocketFlushResult::TryAgain;
|
2018-03-30 22:56:24 +02:00
|
|
|
} else {
|
|
|
|
// We failed to write, but it's possible that we can still read from the socket.
|
|
|
|
// Give that a try before giving up.
|
|
|
|
s->has_write_error = true;
|
2018-03-19 21:20:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we sent the last packet of a closing socket, we can now destroy it.
|
|
|
|
if (s->closing) {
|
|
|
|
s->close(s);
|
|
|
|
return SocketFlushResult::Destroyed;
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent_del(s->fde, FDE_WRITE);
|
2018-03-19 21:20:29 +01:00
|
|
|
return SocketFlushResult::Completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns false if the socket has been closed and destroyed as a side-effect of this function.
|
|
|
|
static bool local_socket_flush_outgoing(asocket* s) {
|
|
|
|
const size_t max_payload = s->get_max_payload();
|
2018-03-08 01:52:28 +01:00
|
|
|
apacket::payload_type data;
|
2018-03-19 21:20:29 +01:00
|
|
|
data.resize(max_payload);
|
|
|
|
char* x = &data[0];
|
|
|
|
size_t avail = max_payload;
|
|
|
|
int r = 0;
|
|
|
|
int is_eof = 0;
|
|
|
|
|
|
|
|
while (avail > 0) {
|
|
|
|
r = adb_read(s->fd, x, avail);
|
|
|
|
D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
|
|
|
|
r < 0 ? errno : 0, avail);
|
|
|
|
if (r == -1) {
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (r > 0) {
|
|
|
|
avail -= r;
|
|
|
|
x += r;
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-02-01 22:17:50 +01:00
|
|
|
|
2018-03-19 21:20:29 +01:00
|
|
|
/* r = 0 or unhandled error */
|
|
|
|
is_eof = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof,
|
2018-05-14 20:14:33 +02:00
|
|
|
s->fde->force_eof);
|
2018-03-19 21:20:29 +01:00
|
|
|
|
|
|
|
if (avail != max_payload && s->peer) {
|
|
|
|
data.resize(max_payload - avail);
|
|
|
|
|
|
|
|
// s->peer->enqueue() may call s->close() and free s,
|
|
|
|
// so save variables for debug printing below.
|
|
|
|
unsigned saved_id = s->id;
|
|
|
|
int saved_fd = s->fd;
|
|
|
|
r = s->peer->enqueue(s->peer, std::move(data));
|
|
|
|
D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
|
|
|
|
|
|
|
|
if (r < 0) {
|
|
|
|
// Error return means they closed us as a side-effect and we must
|
|
|
|
// return immediately.
|
|
|
|
//
|
|
|
|
// Note that if we still have buffered packets, the socket will be
|
|
|
|
// placed on the closing socket list. This handler function will be
|
|
|
|
// called again to process FDE_WRITE events.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r > 0) {
|
|
|
|
/* if the remote cannot accept further events,
|
|
|
|
** we disable notification of READs. They'll
|
|
|
|
** be enabled again when we get a call to ready()
|
|
|
|
*/
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent_del(s->fde, FDE_READ);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 21:20:29 +01:00
|
|
|
// Don't allow a forced eof if data is still there.
|
2018-05-14 20:14:33 +02:00
|
|
|
if ((s->fde->force_eof && !r) || is_eof) {
|
|
|
|
D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde->force_eof);
|
2018-03-19 21:20:29 +01:00
|
|
|
s->close(s);
|
|
|
|
return false;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 21:20:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-08 01:52:28 +01:00
|
|
|
static int local_socket_enqueue(asocket* s, apacket::payload_type data) {
|
2018-03-19 21:20:29 +01:00
|
|
|
D("LS(%d): enqueue %zu", s->id, data.size());
|
|
|
|
|
2018-04-03 23:37:11 +02:00
|
|
|
s->packet_queue.append(std::move(data));
|
2018-03-19 21:20:29 +01:00
|
|
|
switch (local_socket_flush_incoming(s)) {
|
|
|
|
case SocketFlushResult::Destroyed:
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case SocketFlushResult::TryAgain:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case SocketFlushResult::Completed:
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-03-19 21:20:29 +01:00
|
|
|
return !s->packet_queue.empty();
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
static void local_socket_ready(asocket* s) {
|
2015-02-18 12:53:37 +01:00
|
|
|
/* far side is ready for data, pay attention to
|
|
|
|
readable events */
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent_add(s->fde, FDE_READ);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 23:42:08 +01:00
|
|
|
struct ClosingSocket {
|
|
|
|
std::chrono::steady_clock::time_point begin;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The standard (RFC 1122 - 4.2.2.13) says that if we call close on a
|
|
|
|
// socket while we have pending data, a TCP RST should be sent to the
|
|
|
|
// other end to notify it that we didn't read all of its data. However,
|
|
|
|
// this can result in data that we've successfully written out to be dropped
|
|
|
|
// on the other end. To avoid this, instead of immediately closing a
|
|
|
|
// socket, call shutdown on it instead, and then read from the file
|
|
|
|
// descriptor until we hit EOF or an error before closing.
|
|
|
|
static void deferred_close(unique_fd fd) {
|
|
|
|
// Shutdown the socket in the outgoing direction only, so that
|
|
|
|
// we don't have the same problem on the opposite end.
|
|
|
|
adb_shutdown(fd.get(), SHUT_WR);
|
|
|
|
auto callback = [](fdevent* fde, unsigned event, void* arg) {
|
|
|
|
auto socket_info = static_cast<ClosingSocket*>(arg);
|
|
|
|
if (event & FDE_READ) {
|
|
|
|
ssize_t rc;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
while ((rc = adb_read(fde->fd.get(), buf, sizeof(buf))) > 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == -1 && errno == EAGAIN) {
|
|
|
|
// There's potentially more data to read.
|
|
|
|
auto duration = std::chrono::steady_clock::now() - socket_info->begin;
|
|
|
|
if (duration > 1s) {
|
|
|
|
LOG(WARNING) << "timeout expired while flushing socket, closing";
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (event & FDE_TIMEOUT) {
|
|
|
|
LOG(WARNING) << "timeout expired while flushing socket, closing";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Either there was an error, we hit the end of the socket, or our timeout expired.
|
|
|
|
fdevent_destroy(fde);
|
|
|
|
delete socket_info;
|
|
|
|
};
|
|
|
|
|
|
|
|
ClosingSocket* socket_info = new ClosingSocket{
|
|
|
|
.begin = std::chrono::steady_clock::now(),
|
|
|
|
};
|
|
|
|
|
|
|
|
fdevent* fde = fdevent_create(fd.release(), callback, socket_info);
|
|
|
|
fdevent_add(fde, FDE_READ);
|
|
|
|
fdevent_set_timeout(fde, 1s);
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
// be sure to hold the socket list lock when calling this
|
2016-05-18 01:55:06 +02:00
|
|
|
static void local_socket_destroy(asocket* s) {
|
2012-03-16 22:50:07 +01:00
|
|
|
int exit_on_close = s->exit_on_close;
|
|
|
|
|
2018-05-14 20:14:33 +02:00
|
|
|
D("LS(%d): destroying fde.fd=%d", s->id, s->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-01-11 23:42:08 +01:00
|
|
|
deferred_close(fdevent_release(s->fde));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
remove_socket(s);
|
2018-02-13 02:24:00 +01:00
|
|
|
delete s;
|
2012-03-16 22:50:07 +01:00
|
|
|
|
|
|
|
if (exit_on_close) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("local_socket_destroy: exiting");
|
2012-03-16 22:50:07 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-18 02:46:27 +02:00
|
|
|
static void local_socket_close(asocket* s) {
|
|
|
|
D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
|
2016-05-18 01:55:06 +02:00
|
|
|
if (s->peer) {
|
|
|
|
D("LS(%d): closing peer. peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
|
2013-12-13 14:09:44 +01:00
|
|
|
/* Note: it's important to call shutdown before disconnecting from
|
|
|
|
* the peer, this ensures that remote sockets can still get the id
|
|
|
|
* of the local socket they're connected to, to send a CLOSE()
|
|
|
|
* protocol event. */
|
2016-05-18 01:55:06 +02:00
|
|
|
if (s->peer->shutdown) {
|
|
|
|
s->peer->shutdown(s->peer);
|
|
|
|
}
|
2016-05-18 02:46:27 +02:00
|
|
|
s->peer->peer = nullptr;
|
|
|
|
s->peer->close(s->peer);
|
|
|
|
s->peer = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
/* If we are already closing, or if there are no
|
|
|
|
** pending packets, destroy immediately
|
|
|
|
*/
|
2018-02-01 22:17:50 +01:00
|
|
|
if (s->closing || s->has_write_error || s->packet_queue.empty()) {
|
2016-05-18 01:55:06 +02:00
|
|
|
int id = s->id;
|
2009-03-04 04:32:55 +01:00
|
|
|
local_socket_destroy(s);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): closed", id);
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
/* otherwise, put on the closing list
|
|
|
|
*/
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): closing", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->closing = 1;
|
2018-05-14 20:14:33 +02:00
|
|
|
fdevent_del(s->fde, FDE_READ);
|
2009-03-04 04:32:55 +01:00
|
|
|
remove_socket(s);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
|
2018-01-31 22:15:51 +01:00
|
|
|
local_socket_closing_list.push_back(s);
|
2018-05-14 20:14:33 +02:00
|
|
|
CHECK_EQ(FDE_WRITE, s->fde->state & FDE_WRITE);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
static void local_socket_event_func(int fd, unsigned ev, void* _s) {
|
2015-02-26 02:51:28 +01:00
|
|
|
asocket* s = reinterpret_cast<asocket*>(_s);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
|
2011-03-16 23:57:42 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* put the FDE_WRITE processing before the FDE_READ
|
|
|
|
** in order to simplify the code.
|
|
|
|
*/
|
2015-02-26 02:51:28 +01:00
|
|
|
if (ev & FDE_WRITE) {
|
2018-03-19 21:20:29 +01:00
|
|
|
switch (local_socket_flush_incoming(s)) {
|
|
|
|
case SocketFlushResult::Destroyed:
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
|
2018-03-19 21:20:29 +01:00
|
|
|
case SocketFlushResult::TryAgain:
|
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-03-19 21:20:29 +01:00
|
|
|
case SocketFlushResult::Completed:
|
|
|
|
s->peer->ready(s->peer);
|
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
if (ev & FDE_READ) {
|
2018-03-19 21:20:29 +01:00
|
|
|
if (!local_socket_flush_outgoing(s)) {
|
2015-09-16 01:27:09 +02:00
|
|
|
return;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
if (ev & FDE_ERROR) {
|
|
|
|
/* this should be caught be the next read or write
|
|
|
|
** catching it here means we may skip the last few
|
|
|
|
** bytes of readable data.
|
|
|
|
*/
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* create_local_socket(unique_fd ufd) {
|
|
|
|
int fd = ufd.release();
|
2018-02-13 02:24:00 +01:00
|
|
|
asocket* s = new asocket();
|
2009-03-04 04:32:55 +01:00
|
|
|
s->fd = fd;
|
|
|
|
s->enqueue = local_socket_enqueue;
|
|
|
|
s->ready = local_socket_ready;
|
2018-07-14 03:15:16 +02:00
|
|
|
s->shutdown = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = local_socket_close;
|
2011-03-16 23:57:42 +01:00
|
|
|
install_local_socket(s);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-05-14 20:14:33 +02:00
|
|
|
s->fde = fdevent_create(fd, local_socket_event_func, s);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): created (fd=%d)", s->id, s->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-12-13 23:21:00 +01:00
|
|
|
asocket* create_local_service_socket(std::string_view name, atransport* transport) {
|
2009-03-04 04:32:55 +01:00
|
|
|
#if !ADB_HOST
|
2018-11-17 00:40:16 +01:00
|
|
|
if (asocket* s = daemon_service_to_socket(name); s) {
|
|
|
|
return s;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
#endif
|
2019-01-24 00:36:42 +01:00
|
|
|
unique_fd fd = service_to_fd(name, transport);
|
2016-05-18 01:55:06 +02:00
|
|
|
if (fd < 0) {
|
2016-06-15 23:46:56 +02:00
|
|
|
return nullptr;
|
2016-05-18 01:55:06 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2019-01-28 15:17:44 +01:00
|
|
|
int fd_value = fd.get();
|
2019-01-24 00:36:42 +01:00
|
|
|
asocket* s = create_local_socket(std::move(fd));
|
2019-01-28 15:17:44 +01:00
|
|
|
LOG(VERBOSE) << "LS(" << s->id << "): bound to '" << name << "' via " << fd_value;
|
2012-03-16 22:50:07 +01:00
|
|
|
|
2012-03-30 22:19:11 +02:00
|
|
|
#if !ADB_HOST
|
2018-12-13 23:21:00 +01:00
|
|
|
if ((name.starts_with("root:") && getuid() != 0 && __android_log_is_debuggable()) ||
|
|
|
|
(name.starts_with("unroot:") && getuid() == 0) || name.starts_with("usb:") ||
|
|
|
|
name.starts_with("tcpip:")) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): enabling exit_on_close", s->id);
|
2012-03-16 22:50:07 +01:00
|
|
|
s->exit_on_close = 1;
|
|
|
|
}
|
2012-03-30 22:19:11 +02:00
|
|
|
#endif
|
2012-03-16 22:50:07 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ADB_HOST
|
2017-08-17 01:57:01 +02:00
|
|
|
static asocket* create_host_service_socket(const char* name, const char* serial,
|
|
|
|
TransportId transport_id) {
|
2016-05-18 01:55:06 +02:00
|
|
|
asocket* s;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-08-17 01:57:01 +02:00
|
|
|
s = host_service_to_socket(name, serial, transport_id);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-07-14 03:15:16 +02:00
|
|
|
if (s != nullptr) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d) bound to '%s'", s->id, name);
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
#endif /* ADB_HOST */
|
|
|
|
|
2018-03-08 01:52:28 +01:00
|
|
|
static int remote_socket_enqueue(asocket* s, apacket::payload_type data) {
|
2016-05-18 01:55:06 +02:00
|
|
|
D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
|
2018-02-01 22:17:50 +01:00
|
|
|
apacket* p = get_apacket();
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
p->msg.command = A_WRTE;
|
|
|
|
p->msg.arg0 = s->peer->id;
|
|
|
|
p->msg.arg1 = s->id;
|
2018-02-01 22:17:50 +01:00
|
|
|
|
2018-02-06 03:49:10 +01:00
|
|
|
if (data.size() > MAX_PAYLOAD) {
|
2018-02-01 22:17:50 +01:00
|
|
|
put_apacket(p);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-06 03:49:10 +01:00
|
|
|
p->payload = std::move(data);
|
|
|
|
p->msg.data_length = p->payload.size();
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
send_packet(p, s->transport);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
static void remote_socket_ready(asocket* s) {
|
|
|
|
D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
|
|
|
|
apacket* p = get_apacket();
|
2009-03-04 04:32:55 +01:00
|
|
|
p->msg.command = A_OKAY;
|
|
|
|
p->msg.arg0 = s->peer->id;
|
|
|
|
p->msg.arg1 = s->id;
|
|
|
|
send_packet(p, s->transport);
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
static void remote_socket_shutdown(asocket* s) {
|
|
|
|
D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
|
|
|
|
s->peer ? s->peer->fd : -1);
|
|
|
|
apacket* p = get_apacket();
|
2009-03-04 04:32:55 +01:00
|
|
|
p->msg.command = A_CLSE;
|
2016-05-18 01:55:06 +02:00
|
|
|
if (s->peer) {
|
2009-03-04 04:32:55 +01:00
|
|
|
p->msg.arg0 = s->peer->id;
|
2013-12-13 14:09:44 +01:00
|
|
|
}
|
|
|
|
p->msg.arg1 = s->id;
|
|
|
|
send_packet(p, s->transport);
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
static void remote_socket_close(asocket* s) {
|
2013-12-13 14:09:44 +01:00
|
|
|
if (s->peer) {
|
2018-07-14 03:15:16 +02:00
|
|
|
s->peer->peer = nullptr;
|
2016-05-18 01:55:06 +02:00
|
|
|
D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->close(s->peer);
|
|
|
|
}
|
2016-05-18 01:55:06 +02:00
|
|
|
D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
|
|
|
|
s->peer ? s->peer->fd : -1);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("RS(%d): closed", s->id);
|
2018-02-13 02:24:00 +01:00
|
|
|
delete s;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-08-28 03:50:04 +02:00
|
|
|
// Create a remote socket to exchange packets with a remote service through transport
|
|
|
|
// |t|. Where |id| is the socket id of the corresponding service on the other
|
|
|
|
// side of the transport (it is allocated by the remote side and _cannot_ be 0).
|
|
|
|
// Returns a new non-NULL asocket handle.
|
2016-05-18 01:55:06 +02:00
|
|
|
asocket* create_remote_socket(unsigned id, atransport* t) {
|
|
|
|
if (id == 0) {
|
2018-10-19 22:59:44 +02:00
|
|
|
LOG(FATAL) << "invalid remote socket id (0)";
|
2016-05-18 01:55:06 +02:00
|
|
|
}
|
2018-02-13 02:24:00 +01:00
|
|
|
asocket* s = new asocket();
|
2009-03-04 04:32:55 +01:00
|
|
|
s->id = id;
|
|
|
|
s->enqueue = remote_socket_enqueue;
|
|
|
|
s->ready = remote_socket_ready;
|
2013-12-13 14:09:44 +01:00
|
|
|
s->shutdown = remote_socket_shutdown;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = remote_socket_close;
|
|
|
|
s->transport = t;
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("RS(%d): created", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-12-21 02:00:13 +01:00
|
|
|
void connect_to_remote(asocket* s, std::string_view destination) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
|
2016-05-18 01:55:06 +02:00
|
|
|
apacket* p = get_apacket();
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-12-21 02:00:13 +01:00
|
|
|
LOG(VERBOSE) << "LS(" << s->id << ": connect(" << destination << ")";
|
2009-03-04 04:32:55 +01:00
|
|
|
p->msg.command = A_OPEN;
|
|
|
|
p->msg.arg0 = s->id;
|
2018-02-06 03:49:10 +01:00
|
|
|
|
2018-12-21 02:00:13 +01:00
|
|
|
// adbd used to expect a null-terminated string.
|
|
|
|
// Keep doing so to maintain backward compatibility.
|
|
|
|
p->payload.resize(destination.size() + 1);
|
|
|
|
memcpy(p->payload.data(), destination.data(), destination.size());
|
|
|
|
p->payload[destination.size()] = '\0';
|
2018-02-06 03:49:10 +01:00
|
|
|
p->msg.data_length = p->payload.size();
|
|
|
|
|
2018-10-19 22:59:44 +02:00
|
|
|
CHECK_LE(p->msg.data_length, s->get_max_payload());
|
2018-02-06 03:49:10 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
send_packet(p, s->transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is used by magic sockets to rig local sockets to
|
|
|
|
send the go-ahead message when they connect */
|
2016-05-18 01:55:06 +02:00
|
|
|
static void local_socket_ready_notify(asocket* s) {
|
2009-03-04 04:32:55 +01:00
|
|
|
s->ready = local_socket_ready;
|
2018-07-14 03:15:16 +02:00
|
|
|
s->shutdown = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = local_socket_close;
|
2015-05-01 02:32:03 +02:00
|
|
|
SendOkay(s->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->ready(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is used by magic sockets to rig local sockets to
|
|
|
|
send the failure message if they are closed before
|
|
|
|
connected (to avoid closing them without a status message) */
|
2016-05-18 01:55:06 +02:00
|
|
|
static void local_socket_close_notify(asocket* s) {
|
2009-03-04 04:32:55 +01:00
|
|
|
s->ready = local_socket_ready;
|
2018-07-14 03:15:16 +02:00
|
|
|
s->shutdown = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = local_socket_close;
|
2015-05-01 02:32:03 +02:00
|
|
|
SendFail(s->fd, "closed");
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
}
|
|
|
|
|
2018-02-01 22:17:50 +01:00
|
|
|
static unsigned unhex(const char* s, int len) {
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned n = 0, c;
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
while (len-- > 0) {
|
|
|
|
switch ((c = *s++)) {
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
c -= '0';
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
case 'b':
|
|
|
|
case 'c':
|
|
|
|
case 'd':
|
|
|
|
case 'e':
|
|
|
|
case 'f':
|
|
|
|
c = c - 'a' + 10;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
case 'B':
|
|
|
|
case 'C':
|
|
|
|
case 'D':
|
|
|
|
case 'E':
|
|
|
|
case 'F':
|
|
|
|
c = c - 'A' + 10;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0xffffffff;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
n = (n << 4) | c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
#if ADB_HOST
|
|
|
|
|
2016-03-01 17:58:26 +01:00
|
|
|
namespace internal {
|
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
// Parses a host service string of the following format:
|
2016-03-01 17:58:26 +01:00
|
|
|
// * [tcp:|udp:]<serial>[:<port>]:<command>
|
|
|
|
// * <prefix>:<serial>:<command>
|
|
|
|
// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
|
2018-12-19 22:37:41 +01:00
|
|
|
bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
|
|
|
|
std::string_view full_service) {
|
|
|
|
if (full_service.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view serial;
|
|
|
|
std::string_view command = full_service;
|
|
|
|
// Remove |count| bytes from the beginning of command and add them to |serial|.
|
|
|
|
auto consume = [&full_service, &serial, &command](size_t count) {
|
|
|
|
CHECK_LE(count, command.size());
|
|
|
|
if (!serial.empty()) {
|
|
|
|
CHECK_EQ(serial.data() + serial.size(), command.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
serial = full_service.substr(0, serial.size() + count);
|
|
|
|
command.remove_prefix(count);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove the trailing : from serial, and assign the values to the output parameters.
|
|
|
|
auto finish = [out_serial, out_command, &serial, &command] {
|
|
|
|
if (serial.empty() || command.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_EQ(':', serial.back());
|
|
|
|
serial.remove_suffix(1);
|
|
|
|
|
|
|
|
*out_serial = serial;
|
|
|
|
*out_command = command;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr std::string_view prefixes[] = {"usb:", "product:", "model:", "device:"};
|
|
|
|
for (std::string_view prefix : prefixes) {
|
|
|
|
if (command.starts_with(prefix)) {
|
|
|
|
consume(prefix.size());
|
|
|
|
|
|
|
|
size_t offset = command.find_first_of(':');
|
|
|
|
if (offset == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
consume(offset + 1);
|
|
|
|
return finish();
|
2016-03-01 17:58:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For fastboot compatibility, ignore protocol prefixes.
|
2018-12-19 22:37:41 +01:00
|
|
|
if (command.starts_with("tcp:") || command.starts_with("udp:")) {
|
|
|
|
consume(4);
|
|
|
|
if (command.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-31 21:04:23 +02:00
|
|
|
}
|
2019-01-05 03:51:11 +01:00
|
|
|
if (command.starts_with("vsock:")) {
|
|
|
|
// vsock serials are vsock:cid:port, which have an extra colon compared to tcp.
|
|
|
|
size_t next_colon = command.find(':');
|
|
|
|
if (next_colon == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
consume(next_colon + 1);
|
|
|
|
}
|
2012-05-31 21:04:23 +02:00
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
bool found_address = false;
|
|
|
|
if (command[0] == '[') {
|
|
|
|
// Read an IPv6 address. `adb connect` creates the serial number from the canonical
|
|
|
|
// network address so it will always have the [] delimiters.
|
|
|
|
size_t ipv6_end = command.find_first_of(']');
|
|
|
|
if (ipv6_end != std::string::npos) {
|
|
|
|
consume(ipv6_end + 1);
|
|
|
|
if (command.empty()) {
|
|
|
|
// Nothing after the IPv6 address.
|
|
|
|
return false;
|
|
|
|
} else if (command[0] != ':') {
|
|
|
|
// Garbage after the IPv6 address.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
consume(1);
|
|
|
|
found_address = true;
|
2016-09-21 21:08:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
if (!found_address) {
|
|
|
|
// Scan ahead to the next colon.
|
|
|
|
size_t offset = command.find_first_of(':');
|
|
|
|
if (offset == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
consume(offset + 1);
|
2011-03-16 09:43:56 +01:00
|
|
|
}
|
2016-03-01 17:58:26 +01:00
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
// We're either at the beginning of a port, or the command itself.
|
|
|
|
// Look for a port in between colons.
|
|
|
|
size_t next_colon = command.find_first_of(':');
|
|
|
|
if (next_colon == std::string::npos) {
|
|
|
|
// No colon, we must be at the command.
|
|
|
|
return finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool port_valid = true;
|
|
|
|
if (command.size() <= next_colon) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string_view port = command.substr(0, next_colon);
|
|
|
|
for (auto digit : port) {
|
|
|
|
if (!isdigit(digit)) {
|
|
|
|
// Port isn't a number.
|
|
|
|
port_valid = false;
|
|
|
|
break;
|
2011-03-16 09:43:56 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 22:37:41 +01:00
|
|
|
|
|
|
|
if (port_valid) {
|
|
|
|
consume(next_colon + 1);
|
|
|
|
}
|
|
|
|
return finish();
|
2011-03-16 09:43:56 +01:00
|
|
|
}
|
|
|
|
|
2016-03-01 17:58:26 +01:00
|
|
|
} // namespace internal
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
#endif // ADB_HOST
|
2015-05-01 02:32:03 +02:00
|
|
|
|
2018-03-08 01:52:28 +01:00
|
|
|
static int smart_socket_enqueue(asocket* s, apacket::payload_type data) {
|
2009-03-04 04:32:55 +01:00
|
|
|
#if ADB_HOST
|
2018-12-19 22:37:41 +01:00
|
|
|
std::string_view service;
|
|
|
|
std::string_view serial;
|
2017-08-17 01:57:01 +02:00
|
|
|
TransportId transport_id = 0;
|
2015-05-05 22:10:43 +02:00
|
|
|
TransportType type = kTransportAny;
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
|
2018-02-01 22:17:50 +01:00
|
|
|
D("SS(%d): enqueue %zu", s->id, data.size());
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-02-01 22:17:50 +01:00
|
|
|
if (s->smart_socket_data.empty()) {
|
2018-04-03 23:37:11 +02:00
|
|
|
// TODO: Make this an IOVector?
|
2018-03-08 01:52:28 +01:00
|
|
|
s->smart_socket_data.assign(data.begin(), data.end());
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2018-02-01 22:17:50 +01:00
|
|
|
std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-01-15 23:35:54 +01:00
|
|
|
/* don't bother if we can't decode the length */
|
2018-02-01 22:17:50 +01:00
|
|
|
if (s->smart_socket_data.size() < 4) {
|
2016-05-18 01:55:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-02-01 22:17:50 +01:00
|
|
|
uint32_t len = unhex(s->smart_socket_data.data(), 4);
|
|
|
|
if (len == 0 || len > MAX_PAYLOAD) {
|
|
|
|
D("SS(%d): bad size (%u)", s->id, len);
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-02-01 22:17:50 +01:00
|
|
|
D("SS(%d): len is %u", s->id, len);
|
2016-01-15 23:35:54 +01:00
|
|
|
/* can't do anything until we have the full header */
|
2018-02-01 22:17:50 +01:00
|
|
|
if ((len + 4) > s->smart_socket_data.size()) {
|
|
|
|
D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - s->smart_socket_data.size());
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-01 22:17:50 +01:00
|
|
|
s->smart_socket_data[len + 4] = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2018-02-01 22:17:50 +01:00
|
|
|
D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#if ADB_HOST
|
2018-12-19 22:37:41 +01:00
|
|
|
service = std::string_view(s->smart_socket_data).substr(4);
|
|
|
|
if (service.starts_with("host-serial:")) {
|
|
|
|
service.remove_prefix(strlen("host-serial:"));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2011-03-16 09:43:56 +01:00
|
|
|
// serial number should follow "host:" and could be a host:port string.
|
2018-12-19 22:37:41 +01:00
|
|
|
if (!internal::parse_host_service(&serial, &service, service)) {
|
|
|
|
LOG(ERROR) << "SS(" << s->id << "): failed to parse host service: " << service;
|
|
|
|
goto fail;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-12-19 22:37:41 +01:00
|
|
|
} else if (service.starts_with("host-transport-id:")) {
|
|
|
|
service.remove_prefix(strlen("host-transport-id:"));
|
|
|
|
if (!ParseUint(&transport_id, service, &service)) {
|
|
|
|
LOG(ERROR) << "SS(" << s->id << "): failed to parse host transport id: " << service;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!service.starts_with(":")) {
|
|
|
|
LOG(ERROR) << "SS(" << s->id << "): host-transport-id without command";
|
2017-08-17 01:57:01 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-19 22:37:41 +01:00
|
|
|
service.remove_prefix(1);
|
|
|
|
} else if (service.starts_with("host-usb:")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
type = kTransportUsb;
|
2018-12-19 22:37:41 +01:00
|
|
|
service.remove_prefix(strlen("host-usb:"));
|
|
|
|
} else if (service.starts_with("host-local:")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
type = kTransportLocal;
|
2018-12-19 22:37:41 +01:00
|
|
|
service.remove_prefix(strlen("host-local:"));
|
|
|
|
} else if (service.starts_with("host:")) {
|
2015-05-05 22:10:43 +02:00
|
|
|
type = kTransportAny;
|
2018-12-19 22:37:41 +01:00
|
|
|
service.remove_prefix(strlen("host:"));
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2018-12-19 22:37:41 +01:00
|
|
|
service = std::string_view{};
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2018-12-19 22:37:41 +01:00
|
|
|
if (!service.empty()) {
|
2016-05-18 01:55:06 +02:00
|
|
|
asocket* s2;
|
|
|
|
|
2018-08-07 23:14:21 +02:00
|
|
|
// Some requests are handled immediately -- in that case the handle_host_request() routine
|
|
|
|
// has sent the OKAY or FAIL message and all we have to do is clean up.
|
2018-12-19 22:37:41 +01:00
|
|
|
// TODO: Convert to string_view.
|
|
|
|
if (handle_host_request(std::string(service).c_str(), type,
|
|
|
|
serial.empty() ? nullptr : std::string(serial).c_str(),
|
|
|
|
transport_id, s->peer->fd, s)) {
|
|
|
|
LOG(VERBOSE) << "SS(" << s->id << "): handled host service '" << service << "'";
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2018-12-19 22:37:41 +01:00
|
|
|
if (service.starts_with("transport")) {
|
2016-05-18 01:55:06 +02:00
|
|
|
D("SS(%d): okay transport", s->id);
|
2018-02-01 22:17:50 +01:00
|
|
|
s->smart_socket_data.clear();
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
/* try to find a local service with this name.
|
|
|
|
** if no such service exists, we'll fail out
|
|
|
|
** and tear down here.
|
|
|
|
*/
|
2018-12-19 22:37:41 +01:00
|
|
|
// TODO: Convert to string_view.
|
|
|
|
s2 = create_host_service_socket(std::string(service).c_str(), std::string(serial).c_str(),
|
|
|
|
transport_id);
|
2018-07-14 03:15:16 +02:00
|
|
|
if (s2 == nullptr) {
|
2018-12-19 22:37:41 +01:00
|
|
|
LOG(VERBOSE) << "SS(" << s->id << "): couldn't create host service '" << service << "'";
|
2015-05-01 02:32:03 +02:00
|
|
|
SendFail(s->peer->fd, "unknown host service");
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
/* we've connected to a local host service,
|
|
|
|
** so we make our peer back into a regular
|
|
|
|
** local socket and bind it to the new local
|
|
|
|
** service socket, acknowledge the successful
|
|
|
|
** connection, and close this smart socket now
|
|
|
|
** that its work is done.
|
|
|
|
*/
|
2015-05-01 02:32:03 +02:00
|
|
|
SendOkay(s->peer->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
s->peer->ready = local_socket_ready;
|
2015-10-07 23:55:10 +02:00
|
|
|
s->peer->shutdown = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->close = local_socket_close;
|
|
|
|
s->peer->peer = s2;
|
|
|
|
s2->peer = s->peer;
|
2018-07-14 03:15:16 +02:00
|
|
|
s->peer = nullptr;
|
2016-05-18 01:55:06 +02:00
|
|
|
D("SS(%d): okay", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
/* initial state is "ready" */
|
2009-03-04 04:32:55 +01:00
|
|
|
s2->ready(s2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* !ADB_HOST */
|
2015-10-07 23:55:10 +02:00
|
|
|
if (s->transport == nullptr) {
|
2015-04-17 07:54:44 +02:00
|
|
|
std::string error_msg = "unknown failure";
|
2017-08-17 01:57:01 +02:00
|
|
|
s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
|
2015-10-07 23:55:10 +02:00
|
|
|
if (s->transport == nullptr) {
|
2015-05-01 02:32:03 +02:00
|
|
|
SendFail(s->peer->fd, error_msg);
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-27 23:01:08 +02:00
|
|
|
if (!s->transport) {
|
|
|
|
SendFail(s->peer->fd, "device offline (no transport)");
|
|
|
|
goto fail;
|
2018-05-05 01:04:49 +02:00
|
|
|
} else if (!ConnectionStateIsOnline(s->transport->GetConnectionState())) {
|
2016-05-18 01:55:06 +02:00
|
|
|
/* if there's no remote we fail the connection
|
|
|
|
** right here and terminate it
|
|
|
|
*/
|
2016-10-27 23:01:08 +02:00
|
|
|
SendFail(s->peer->fd, "device offline (transport offline)");
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
/* instrument our peer to pass the success or fail
|
|
|
|
** message back once it connects or closes, then
|
|
|
|
** detach from it, request the connection, and
|
|
|
|
** tear down
|
|
|
|
*/
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->ready = local_socket_ready_notify;
|
2015-10-07 23:55:10 +02:00
|
|
|
s->peer->shutdown = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->close = local_socket_close_notify;
|
2018-07-14 03:15:16 +02:00
|
|
|
s->peer->peer = nullptr;
|
2016-05-18 01:55:06 +02:00
|
|
|
/* give him our transport and upref it */
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->transport = s->transport;
|
|
|
|
|
2018-12-21 02:00:13 +01:00
|
|
|
connect_to_remote(s->peer, std::string_view(s->smart_socket_data).substr(4));
|
2018-07-14 03:15:16 +02:00
|
|
|
s->peer = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
fail:
|
2016-05-18 01:55:06 +02:00
|
|
|
/* we're going to close our peer as a side-effect, so
|
|
|
|
** return -1 to signal that state to the local socket
|
|
|
|
** who is enqueueing against us
|
|
|
|
*/
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
static void smart_socket_ready(asocket* s) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): ready", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
static void smart_socket_close(asocket* s) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): closed", s->id);
|
2016-05-18 01:55:06 +02:00
|
|
|
if (s->peer) {
|
2018-07-14 03:15:16 +02:00
|
|
|
s->peer->peer = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->close(s->peer);
|
2018-07-14 03:15:16 +02:00
|
|
|
s->peer = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2018-02-13 02:24:00 +01:00
|
|
|
delete s;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
static asocket* create_smart_socket(void) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Creating smart socket");
|
2018-02-13 02:24:00 +01:00
|
|
|
asocket* s = new asocket();
|
2009-03-04 04:32:55 +01:00
|
|
|
s->enqueue = smart_socket_enqueue;
|
|
|
|
s->ready = smart_socket_ready;
|
2018-07-14 03:15:16 +02:00
|
|
|
s->shutdown = nullptr;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = smart_socket_close;
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d)", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-05-18 01:55:06 +02:00
|
|
|
void connect_to_smartsocket(asocket* s) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Connecting to smart socket");
|
2016-05-18 01:55:06 +02:00
|
|
|
asocket* ss = create_smart_socket();
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer = ss;
|
|
|
|
ss->peer = s;
|
|
|
|
s->ready(s);
|
|
|
|
}
|
2015-07-13 20:12:28 +02:00
|
|
|
|
|
|
|
size_t asocket::get_max_payload() const {
|
|
|
|
size_t max_payload = MAX_PAYLOAD;
|
|
|
|
if (transport) {
|
|
|
|
max_payload = std::min(max_payload, transport->get_max_payload());
|
|
|
|
}
|
|
|
|
if (peer && peer->transport) {
|
|
|
|
max_payload = std::min(max_payload, peer->transport->get_max_payload());
|
|
|
|
}
|
|
|
|
return max_payload;
|
|
|
|
}
|