• 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 #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 (serverfd < 0) {
60             if (errno == EAFNOSUPPORT || errno == EINVAL || errno == EPROTONOSUPPORT) {
61                 D("unrecoverable error: '%s'", error.c_str());
62                 return;
63             }
64             D("server: cannot bind socket yet: %s", error.c_str());
65             std::this_thread::sleep_for(1s);
66             continue;
67         }
68         close_on_exec(serverfd.get());
69     }
70 
71     while (true) {
72         D("server: trying to get new connection from fd %d", serverfd.get());
73         unique_fd fd(adb_socket_accept(serverfd, nullptr, nullptr));
74         if (fd >= 0) {
75             D("server: new connection on fd %d", fd.get());
76             close_on_exec(fd.get());
77             disable_tcp_nagle(fd.get());
78             std::string serial = android::base::StringPrintf("host-%d", fd.get());
79             // We don't care about port value in "register_socket_transport" as it is used
80             // only from ADB_HOST. "server_socket_thread" is never called from ADB_HOST.
81             register_socket_transport(
82                     std::move(fd), std::move(serial), 0, false,
83                     [](atransport*) { return ReconnectResult::Abort; }, false);
84         }
85     }
86     D("transport: server_socket_thread() exiting");
87 }
88 
init_transport_socket_server(const std::string & addr)89 void init_transport_socket_server(const std::string& addr) {
90     VLOG(TRANSPORT) << "Starting tcp server on '" << addr << "'";
91     std::thread(server_socket_thread, addr).detach();
92 }
93 
init_socket_transport(atransport * t,unique_fd fd,int,bool)94 int init_socket_transport(atransport* t, unique_fd fd, int, bool) {
95     t->type = kTransportLocal;
96     auto fd_connection = std::make_unique<FdConnection>(std::move(fd));
97     t->SetConnection(std::make_unique<BlockingConnectionAdapter>(std::move(fd_connection)));
98     return 0;
99 }