1 /*
2 * Copyright (C) 2007 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 #define TRACE_TAG TRANSPORT
18
19 #include "sysdeps.h"
20 #include "transport.h"
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #include <condition_variable>
29 #include <functional>
30 #include <memory>
31 #include <mutex>
32 #include <thread>
33 #include <unordered_map>
34 #include <vector>
35
36 #include <android-base/parsenetaddress.h>
37 #include <android-base/stringprintf.h>
38 #include <android-base/thread_annotations.h>
39 #include <cutils/sockets.h>
40
41 #include <android-base/properties.h>
42
43 #include "adb.h"
44 #include "adb_io.h"
45 #include "adb_unique_fd.h"
46 #include "adb_utils.h"
47 #include "socket_spec.h"
48 #include "sysdeps/chrono.h"
49
server_socket_thread(std::string_view addr)50 void server_socket_thread(std::string_view addr) {
51 adb_thread_setname("server_socket");
52
53 unique_fd serverfd;
54 std::string error;
55
56 while (serverfd == -1) {
57 errno = 0;
58 serverfd = unique_fd{socket_spec_listen(addr, &error, nullptr)};
59 if (errno == EAFNOSUPPORT || errno == EINVAL || errno == EPROTONOSUPPORT) {
60 D("unrecoverable error: '%s'", error.c_str());
61 return;
62 } else if (serverfd < 0) {
63 D("server: cannot bind socket yet: %s", error.c_str());
64 std::this_thread::sleep_for(1s);
65 continue;
66 }
67 close_on_exec(serverfd.get());
68 }
69
70 while (true) {
71 D("server: trying to get new connection from fd %d", serverfd.get());
72 unique_fd fd(adb_socket_accept(serverfd, nullptr, nullptr));
73 if (fd >= 0) {
74 D("server: new connection on fd %d", fd.get());
75 close_on_exec(fd.get());
76 disable_tcp_nagle(fd.get());
77 std::string serial = android::base::StringPrintf("host-%d", fd.get());
78 // We don't care about port value in "register_socket_transport" as it is used
79 // only from ADB_HOST. "server_socket_thread" is never called from ADB_HOST.
80 register_socket_transport(
81 std::move(fd), std::move(serial), 0, false,
82 [](atransport*) { return ReconnectResult::Abort; }, false);
83 }
84 }
85 D("transport: server_socket_thread() exiting");
86 }
87
init_transport_socket_server(const std::string & addr)88 void init_transport_socket_server(const std::string& addr) {
89 VLOG(TRANSPORT) << "Starting tcp server on '" << addr << "'";
90 std::thread(server_socket_thread, addr).detach();
91 }
92
init_socket_transport(atransport * t,unique_fd fd,int,bool)93 int init_socket_transport(atransport* t, unique_fd fd, int, bool) {
94 t->type = kTransportLocal;
95 auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
96 t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection)));
97 return 0;
98 }