• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if !ADB_HOST
42 #include <android-base/properties.h>
43 #endif
44 
45 #include "adb.h"
46 #include "adb_io.h"
47 #include "adb_unique_fd.h"
48 #include "adb_utils.h"
49 #include "socket_spec.h"
50 #include "sysdeps/chrono.h"
51 
server_socket_thread(std::function<unique_fd (std::string_view,std::string *)> listen_func,std::string_view addr)52 void server_socket_thread(std::function<unique_fd(std::string_view, std::string*)> listen_func,
53                           std::string_view addr) {
54     adb_thread_setname("server socket");
55 
56     unique_fd serverfd;
57     std::string error;
58 
59     while (serverfd == -1) {
60         errno = 0;
61         serverfd = listen_func(addr, &error);
62         if (errno == EAFNOSUPPORT || errno == EINVAL || errno == EPROTONOSUPPORT) {
63             D("unrecoverable error: '%s'", error.c_str());
64             return;
65         } else if (serverfd < 0) {
66             D("server: cannot bind socket yet: %s", error.c_str());
67             std::this_thread::sleep_for(1s);
68             continue;
69         }
70         close_on_exec(serverfd.get());
71     }
72 
73     while (true) {
74         D("server: trying to get new connection from fd %d", serverfd.get());
75         unique_fd fd(adb_socket_accept(serverfd, nullptr, nullptr));
76         if (fd >= 0) {
77             D("server: new connection on fd %d", fd.get());
78             close_on_exec(fd.get());
79             disable_tcp_nagle(fd.get());
80             std::string serial = android::base::StringPrintf("host-%d", fd.get());
81             // We don't care about port value in "register_socket_transport" as it is used
82             // only from ADB_HOST. "server_socket_thread" is never called from ADB_HOST.
83             register_socket_transport(
84                     std::move(fd), std::move(serial), 0, 1,
85                     [](atransport*) { return ReconnectResult::Abort; }, false);
86         }
87     }
88     D("transport: server_socket_thread() exiting");
89 }
90 
adb_listen(std::string_view addr,std::string * error)91 unique_fd adb_listen(std::string_view addr, std::string* error) {
92     return unique_fd{socket_spec_listen(addr, error, nullptr)};
93 }
94 
local_init(const std::string & addr)95 void local_init(const std::string& addr) {
96     D("transport: local server init");
97     std::thread(server_socket_thread, adb_listen, addr).detach();
98 }
99 
init_socket_transport(atransport * t,unique_fd fd,int adb_port,int local)100 int init_socket_transport(atransport* t, unique_fd fd, int adb_port, int local) {
101     t->type = kTransportLocal;
102     auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
103     t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection)));
104     return 0;
105 }
106