blob: 0007fed7ba715e34800429f7c8cf0e4712b1836b [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG SOCKETS
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Dan Albert76649012015-02-24 15:51:19 -080021#include <ctype.h>
22#include <errno.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <stdio.h>
24#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <string.h>
Dan Albert76649012015-02-24 15:51:19 -080026#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
Spencer Low363af562015-11-07 18:51:54 -080028#include <algorithm>
Josh Gao9b587de2016-05-17 17:46:27 -070029#include <mutex>
David Pursell3f902aa2016-03-01 08:58:26 -080030#include <string>
31#include <vector>
Spencer Low363af562015-11-07 18:51:54 -080032
Dan Albert76649012015-02-24 15:51:19 -080033#if !ADB_HOST
Elliott Hughesffdec182016-09-23 15:40:03 -070034#include <android-base/properties.h>
Steven Morelandd73be1b2017-04-13 23:48:57 -070035#include <log/log_properties.h>
Dan Albert76649012015-02-24 15:51:19 -080036#endif
Dan Albert33134262015-03-19 15:21:08 -070037
38#include "adb.h"
39#include "adb_io.h"
Josh Gao27cb7dc2018-02-01 13:17:50 -080040#include "range.h"
Dan Albert76649012015-02-24 15:51:19 -080041#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Josh Gao9b587de2016-05-17 17:46:27 -070043static std::recursive_mutex& local_socket_list_lock = *new std::recursive_mutex();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044static unsigned local_socket_next_id = 1;
45
Josh Gao5e507642018-01-31 13:15:51 -080046static auto& local_socket_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
48/* the the list of currently closing local sockets.
49** these have no peer anymore, but still packets to
50** write to their fd.
51*/
Josh Gao5e507642018-01-31 13:15:51 -080052static auto& local_socket_closing_list = *new std::vector<asocket*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
David 'Digit' Turner818d6412013-12-13 14:09:44 +010054// Parse the global list of sockets to find one with id |local_id|.
55// If |peer_id| is not 0, also check that it is connected to a peer
56// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
Josh Gao52bd8522016-05-17 16:55:06 -070057asocket* find_local_socket(unsigned local_id, unsigned peer_id) {
Josh Gao5e507642018-01-31 13:15:51 -080058 asocket* result = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Josh Gao9b587de2016-05-17 17:46:27 -070060 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-01-31 13:15:51 -080061 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -070062 if (s->id != local_id) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +010063 continue;
Josh Gao52bd8522016-05-17 16:55:06 -070064 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010065 if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
André Goddard Rosa81828292010-06-12 11:40:20 -030066 result = s;
André Goddard Rosa81828292010-06-12 11:40:20 -030067 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010068 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
71 return result;
72}
73
Josh Gao52bd8522016-05-17 16:55:06 -070074void install_local_socket(asocket* s) {
Josh Gao9b587de2016-05-17 17:46:27 -070075 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
77 s->id = local_socket_next_id++;
David 'Digit' Turner818d6412013-12-13 14:09:44 +010078
79 // Socket ids should never be 0.
Josh Gao52bd8522016-05-17 16:55:06 -070080 if (local_socket_next_id == 0) {
Josh Gao9b587de2016-05-17 17:46:27 -070081 fatal("local socket id overflow");
Josh Gao52bd8522016-05-17 16:55:06 -070082 }
David 'Digit' Turner818d6412013-12-13 14:09:44 +010083
Josh Gao5e507642018-01-31 13:15:51 -080084 local_socket_list.push_back(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085}
86
Josh Gao52bd8522016-05-17 16:55:06 -070087void remove_socket(asocket* s) {
Josh Gao62c92f02017-09-13 11:17:33 -070088 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao5e507642018-01-31 13:15:51 -080089 for (auto list : { &local_socket_list, &local_socket_closing_list }) {
90 list->erase(std::remove_if(list->begin(), list->end(), [s](asocket* x) { return x == s; }),
91 list->end());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092 }
93}
94
Josh Gao52bd8522016-05-17 16:55:06 -070095void close_all_sockets(atransport* t) {
Josh Gao52bd8522016-05-17 16:55:06 -070096 /* this is a little gross, but since s->close() *will* modify
97 ** the list out from under you, your options are limited.
98 */
Josh Gao9b587de2016-05-17 17:46:27 -070099 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100restart:
Josh Gao5e507642018-01-31 13:15:51 -0800101 for (asocket* s : local_socket_list) {
Josh Gao52bd8522016-05-17 16:55:06 -0700102 if (s->transport == t || (s->peer && s->peer->transport == t)) {
Josh Gao53eb31d2016-05-18 10:39:48 -0700103 s->close(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 goto restart;
105 }
106 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107}
108
Josh Gao27cb7dc2018-02-01 13:17:50 -0800109static int local_socket_enqueue(asocket* s, std::string data) {
110 D("LS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111
Josh Gao27cb7dc2018-02-01 13:17:50 -0800112 Range r(std::move(data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113
Josh Gao52bd8522016-05-17 16:55:06 -0700114 /* if there is already data queue'd, we will receive
115 ** events when it's time to write. just add this to
116 ** the tail
117 */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800118 if (!s->packet_queue.empty()) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 goto enqueue;
120 }
121
Josh Gao52bd8522016-05-17 16:55:06 -0700122 /* write as much as we can, until we
123 ** would block or there is an error/eof
124 */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800125 while (!r.empty()) {
126 int rc = adb_write(s->fd, r.data(), r.size());
127 if (rc > 0) {
128 r.drop_front(rc);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 continue;
130 }
Josh Gao27cb7dc2018-02-01 13:17:50 -0800131
132 if (rc == 0 || errno != EAGAIN) {
Josh Gao52bd8522016-05-17 16:55:06 -0700133 D("LS(%d): not ready, errno=%d: %s", s->id, errno, strerror(errno));
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700134 s->has_write_error = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 s->close(s);
136 return 1; /* not ready (error) */
137 } else {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800138 // errno == EAGAIN
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 break;
140 }
141 }
142
Josh Gao27cb7dc2018-02-01 13:17:50 -0800143 if (r.empty()) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 return 0; /* ready for more data */
145 }
146
147enqueue:
Josh Gao52bd8522016-05-17 16:55:06 -0700148 /* make sure we are notified when we can drain the queue */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800149 s->packet_queue.push_back(std::move(r));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 fdevent_add(&s->fde, FDE_WRITE);
151
152 return 1; /* not ready (backlog) */
153}
154
Josh Gao52bd8522016-05-17 16:55:06 -0700155static void local_socket_ready(asocket* s) {
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100156 /* far side is ready for data, pay attention to
157 readable events */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 fdevent_add(&s->fde, FDE_READ);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159}
160
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161// be sure to hold the socket list lock when calling this
Josh Gao52bd8522016-05-17 16:55:06 -0700162static void local_socket_destroy(asocket* s) {
Benoit Gobyf366b362012-03-16 14:50:07 -0700163 int exit_on_close = s->exit_on_close;
164
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700165 D("LS(%d): destroying fde.fd=%d", s->id, s->fde.fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Josh Gao52bd8522016-05-17 16:55:06 -0700167 /* IMPORTANT: the remove closes the fd
168 ** that belongs to this socket
169 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 fdevent_remove(&s->fde);
171
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 remove_socket(s);
Josh Gaoe0361d12018-02-12 17:24:00 -0800173 delete s;
Benoit Gobyf366b362012-03-16 14:50:07 -0700174
175 if (exit_on_close) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700176 D("local_socket_destroy: exiting");
Benoit Gobyf366b362012-03-16 14:50:07 -0700177 exit(1);
178 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179}
180
Josh Gao9b587de2016-05-17 17:46:27 -0700181static void local_socket_close(asocket* s) {
182 D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd);
183 std::lock_guard<std::recursive_mutex> lock(local_socket_list_lock);
Josh Gao52bd8522016-05-17 16:55:06 -0700184 if (s->peer) {
185 D("LS(%d): closing peer. peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100186 /* Note: it's important to call shutdown before disconnecting from
187 * the peer, this ensures that remote sockets can still get the id
188 * of the local socket they're connected to, to send a CLOSE()
189 * protocol event. */
Josh Gao52bd8522016-05-17 16:55:06 -0700190 if (s->peer->shutdown) {
191 s->peer->shutdown(s->peer);
192 }
Josh Gao9b587de2016-05-17 17:46:27 -0700193 s->peer->peer = nullptr;
194 s->peer->close(s->peer);
195 s->peer = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 }
197
Josh Gao52bd8522016-05-17 16:55:06 -0700198 /* If we are already closing, or if there are no
199 ** pending packets, destroy immediately
200 */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800201 if (s->closing || s->has_write_error || s->packet_queue.empty()) {
Josh Gao52bd8522016-05-17 16:55:06 -0700202 int id = s->id;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 local_socket_destroy(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700204 D("LS(%d): closed", id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 return;
206 }
207
Josh Gao52bd8522016-05-17 16:55:06 -0700208 /* otherwise, put on the closing list
209 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700210 D("LS(%d): closing", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 s->closing = 1;
212 fdevent_del(&s->fde, FDE_READ);
213 remove_socket(s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700214 D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
Josh Gao5e507642018-01-31 13:15:51 -0800215 local_socket_closing_list.push_back(s);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700216 CHECK_EQ(FDE_WRITE, s->fde.state & FDE_WRITE);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217}
218
Josh Gao52bd8522016-05-17 16:55:06 -0700219static void local_socket_event_func(int fd, unsigned ev, void* _s) {
Dan Albertbac34742015-02-25 17:51:28 -0800220 asocket* s = reinterpret_cast<asocket*>(_s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700221 D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
JP Abgrall408fa572011-03-16 15:57:42 -0700222
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 /* put the FDE_WRITE processing before the FDE_READ
224 ** in order to simplify the code.
225 */
Dan Albertbac34742015-02-25 17:51:28 -0800226 if (ev & FDE_WRITE) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800227 while (!s->packet_queue.empty()) {
228 Range& r = s->packet_queue.front();
229 while (!r.empty()) {
230 int rc = adb_write(fd, r.data(), r.size());
231 if (rc == -1) {
Dan Albertbac34742015-02-25 17:51:28 -0800232 /* returning here is ok because FDE_READ will
233 ** be processed in the next iteration loop
234 */
235 if (errno == EAGAIN) {
236 return;
237 }
Josh Gao27cb7dc2018-02-01 13:17:50 -0800238 } else if (rc > 0) {
239 r.drop_front(rc);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 continue;
241 }
Dan Albertbac34742015-02-25 17:51:28 -0800242
Josh Gao27cb7dc2018-02-01 13:17:50 -0800243 D(" closing after write because rc=%d and errno is %d", rc, errno);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700244 s->has_write_error = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 s->close(s);
246 return;
247 }
248
Josh Gao27cb7dc2018-02-01 13:17:50 -0800249 if (r.empty()) {
250 s->packet_queue.pop_front();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 }
252 }
253
Dan Albertbac34742015-02-25 17:51:28 -0800254 /* if we sent the last packet of a closing socket,
255 ** we can now destroy it.
256 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 if (s->closing) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700258 D(" closing because 'closing' is set after write");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 s->close(s);
260 return;
261 }
262
Dan Albertbac34742015-02-25 17:51:28 -0800263 /* no more packets queued, so we can ignore
264 ** writable events again and tell our peer
265 ** to resume writing
266 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 fdevent_del(&s->fde, FDE_WRITE);
268 s->peer->ready(s->peer);
269 }
270
Dan Albertbac34742015-02-25 17:51:28 -0800271 if (ev & FDE_READ) {
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100272 const size_t max_payload = s->get_max_payload();
Josh Gao27cb7dc2018-02-01 13:17:50 -0800273 std::string data;
274 data.resize(max_payload);
275 char* x = &data[0];
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100276 size_t avail = max_payload;
277 int r = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 int is_eof = 0;
279
Dan Albertbac34742015-02-25 17:51:28 -0800280 while (avail > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 r = adb_read(fd, x, avail);
Josh Gao52bd8522016-05-17 16:55:06 -0700282 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu", s->id, s->fd, r,
283 r < 0 ? errno : 0, avail);
Dan Albertbac34742015-02-25 17:51:28 -0800284 if (r == -1) {
285 if (errno == EAGAIN) {
286 break;
287 }
288 } else if (r > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 avail -= r;
290 x += r;
291 continue;
292 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293
Dan Albertbac34742015-02-25 17:51:28 -0800294 /* r = 0 or unhandled error */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 is_eof = 1;
296 break;
297 }
Josh Gao52bd8522016-05-17 16:55:06 -0700298 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d", s->id, s->fd, r, is_eof,
299 s->fde.force_eof);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800300
301 if (avail != max_payload && s->peer) {
302 data.resize(max_payload - avail);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303
Yabin Cui00674122015-08-26 12:27:40 -0700304 // s->peer->enqueue() may call s->close() and free s,
305 // so save variables for debug printing below.
306 unsigned saved_id = s->id;
307 int saved_fd = s->fd;
Josh Gao27cb7dc2018-02-01 13:17:50 -0800308 r = s->peer->enqueue(s->peer, std::move(data));
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700309 D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310
Dan Albertbac34742015-02-25 17:51:28 -0800311 if (r < 0) {
Josh Gao52bd8522016-05-17 16:55:06 -0700312 /* error return means they closed us as a side-effect
313 ** and we must return immediately.
314 **
315 ** note that if we still have buffered packets, the
316 ** socket will be placed on the closing socket list.
317 ** this handler function will be called again
318 ** to process FDE_WRITE events.
319 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 return;
321 }
322
Dan Albertbac34742015-02-25 17:51:28 -0800323 if (r > 0) {
Josh Gao52bd8522016-05-17 16:55:06 -0700324 /* if the remote cannot accept further events,
325 ** we disable notification of READs. They'll
326 ** be enabled again when we get a call to ready()
327 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 fdevent_del(&s->fde, FDE_READ);
329 }
330 }
JP Abgrall112445b2011-04-12 22:01:58 -0700331 /* Don't allow a forced eof if data is still there */
Dan Albertbac34742015-02-25 17:51:28 -0800332 if ((s->fde.force_eof && !r) || is_eof) {
Josh Gao52bd8522016-05-17 16:55:06 -0700333 D(" closing because is_eof=%d r=%d s->fde.force_eof=%d", is_eof, r, s->fde.force_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 s->close(s);
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700335 return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 }
337 }
338
Josh Gao52bd8522016-05-17 16:55:06 -0700339 if (ev & FDE_ERROR) {
340 /* this should be caught be the next read or write
341 ** catching it here means we may skip the last few
342 ** bytes of readable data.
343 */
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700344 D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 return;
346 }
347}
348
Josh Gao52bd8522016-05-17 16:55:06 -0700349asocket* create_local_socket(int fd) {
Josh Gaoe0361d12018-02-12 17:24:00 -0800350 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 s->fd = fd;
352 s->enqueue = local_socket_enqueue;
353 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100354 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700356 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357
358 fdevent_install(&s->fde, fd, local_socket_event_func, s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700359 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 return s;
361}
362
Josh Gao52bd8522016-05-17 16:55:06 -0700363asocket* create_local_service_socket(const char* name, const atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364#if !ADB_HOST
Josh Gao52bd8522016-05-17 16:55:06 -0700365 if (!strcmp(name, "jdwp")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 return create_jdwp_service_socket();
367 }
Josh Gao52bd8522016-05-17 16:55:06 -0700368 if (!strcmp(name, "track-jdwp")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369 return create_jdwp_tracker_service_socket();
370 }
371#endif
David Pursell0955c662015-08-31 10:42:13 -0700372 int fd = service_to_fd(name, transport);
Josh Gao52bd8522016-05-17 16:55:06 -0700373 if (fd < 0) {
Elliott Hughesffc73a32016-06-15 14:46:56 -0700374 return nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700375 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376
Dan Pasanen98858812014-10-06 12:57:20 -0500377 asocket* s = create_local_socket(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700378 D("LS(%d): bound to '%s' via %d", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700379
JP Abgrallf91259a2012-03-30 13:19:11 -0700380#if !ADB_HOST
Mark Salyzyn97787a02016-03-28 15:52:13 -0700381 if ((!strncmp(name, "root:", 5) && getuid() != 0 && __android_log_is_debuggable()) ||
Josh Gao52bd8522016-05-17 16:55:06 -0700382 (!strncmp(name, "unroot:", 7) && getuid() == 0) ||
383 !strncmp(name, "usb:", 4) ||
384 !strncmp(name, "tcpip:", 6)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700385 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700386 s->exit_on_close = 1;
387 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700388#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700389
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 return s;
391}
392
393#if ADB_HOST
Josh Gaob122b172017-08-16 16:57:01 -0700394static asocket* create_host_service_socket(const char* name, const char* serial,
395 TransportId transport_id) {
Josh Gao52bd8522016-05-17 16:55:06 -0700396 asocket* s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397
Josh Gaob122b172017-08-16 16:57:01 -0700398 s = host_service_to_socket(name, serial, transport_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399
400 if (s != NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700401 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 return s;
403 }
404
405 return s;
406}
407#endif /* ADB_HOST */
408
Josh Gao27cb7dc2018-02-01 13:17:50 -0800409static int remote_socket_enqueue(asocket* s, std::string data) {
Josh Gao52bd8522016-05-17 16:55:06 -0700410 D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800411 apacket* p = get_apacket();
412
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 p->msg.command = A_WRTE;
414 p->msg.arg0 = s->peer->id;
415 p->msg.arg1 = s->id;
Josh Gao27cb7dc2018-02-01 13:17:50 -0800416
Josh Gaof571fcb2018-02-05 18:49:10 -0800417 if (data.size() > MAX_PAYLOAD) {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800418 put_apacket(p);
419 return -1;
420 }
421
Josh Gaof571fcb2018-02-05 18:49:10 -0800422 p->payload = std::move(data);
423 p->msg.data_length = p->payload.size();
424
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 send_packet(p, s->transport);
426 return 1;
427}
428
Josh Gao52bd8522016-05-17 16:55:06 -0700429static void remote_socket_ready(asocket* s) {
430 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
431 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 p->msg.command = A_OKAY;
433 p->msg.arg0 = s->peer->id;
434 p->msg.arg1 = s->id;
435 send_packet(p, s->transport);
436}
437
Josh Gao52bd8522016-05-17 16:55:06 -0700438static void remote_socket_shutdown(asocket* s) {
439 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
440 s->peer ? s->peer->fd : -1);
441 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 p->msg.command = A_CLSE;
Josh Gao52bd8522016-05-17 16:55:06 -0700443 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100445 }
446 p->msg.arg1 = s->id;
447 send_packet(p, s->transport);
448}
449
Josh Gao52bd8522016-05-17 16:55:06 -0700450static void remote_socket_close(asocket* s) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100451 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 s->peer->peer = 0;
Josh Gao52bd8522016-05-17 16:55:06 -0700453 D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 s->peer->close(s->peer);
455 }
Josh Gao52bd8522016-05-17 16:55:06 -0700456 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
457 s->peer ? s->peer->fd : -1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700458 D("RS(%d): closed", s->id);
Josh Gaoe0361d12018-02-12 17:24:00 -0800459 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460}
461
Yabin Cuifd28f322015-08-27 18:50:04 -0700462// Create a remote socket to exchange packets with a remote service through transport
463// |t|. Where |id| is the socket id of the corresponding service on the other
464// side of the transport (it is allocated by the remote side and _cannot_ be 0).
465// Returns a new non-NULL asocket handle.
Josh Gao52bd8522016-05-17 16:55:06 -0700466asocket* create_remote_socket(unsigned id, atransport* t) {
467 if (id == 0) {
468 fatal("invalid remote socket id (0)");
469 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800470 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 s->id = id;
472 s->enqueue = remote_socket_enqueue;
473 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100474 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 s->close = remote_socket_close;
476 s->transport = t;
477
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700478 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 return s;
480}
481
Josh Gao52bd8522016-05-17 16:55:06 -0700482void connect_to_remote(asocket* s, const char* destination) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700483 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gao52bd8522016-05-17 16:55:06 -0700484 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700486 D("LS(%d): connect('%s')", s->id, destination);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 p->msg.command = A_OPEN;
488 p->msg.arg0 = s->id;
Josh Gaof571fcb2018-02-05 18:49:10 -0800489
490 // adbd expects a null-terminated string.
491 p->payload = destination;
492 p->payload.push_back('\0');
493 p->msg.data_length = p->payload.size();
494
495 if (p->msg.data_length > s->get_max_payload()) {
496 fatal("destination oversized");
497 }
498
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 send_packet(p, s->transport);
500}
501
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502/* this is used by magic sockets to rig local sockets to
503 send the go-ahead message when they connect */
Josh Gao52bd8522016-05-17 16:55:06 -0700504static void local_socket_ready_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100506 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700508 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 s->ready(s);
510}
511
512/* this is used by magic sockets to rig local sockets to
513 send the failure message if they are closed before
514 connected (to avoid closing them without a status message) */
Josh Gao52bd8522016-05-17 16:55:06 -0700515static void local_socket_close_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100517 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700519 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 s->close(s);
521}
522
Josh Gao27cb7dc2018-02-01 13:17:50 -0800523static unsigned unhex(const char* s, int len) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 unsigned n = 0, c;
525
Josh Gao52bd8522016-05-17 16:55:06 -0700526 while (len-- > 0) {
527 switch ((c = *s++)) {
528 case '0':
529 case '1':
530 case '2':
531 case '3':
532 case '4':
533 case '5':
534 case '6':
535 case '7':
536 case '8':
537 case '9':
538 c -= '0';
539 break;
540 case 'a':
541 case 'b':
542 case 'c':
543 case 'd':
544 case 'e':
545 case 'f':
546 c = c - 'a' + 10;
547 break;
548 case 'A':
549 case 'B':
550 case 'C':
551 case 'D':
552 case 'E':
553 case 'F':
554 c = c - 'A' + 10;
555 break;
556 default:
557 return 0xffffffff;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 }
559
560 n = (n << 4) | c;
561 }
562
563 return n;
564}
565
Elliott Hughese67f1f82015-04-30 17:32:03 -0700566#if ADB_HOST
567
David Pursell3f902aa2016-03-01 08:58:26 -0800568namespace internal {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700569
David Pursell3f902aa2016-03-01 08:58:26 -0800570// Returns the position in |service| following the target serial parameter. Serial format can be
571// any of:
572// * [tcp:|udp:]<serial>[:<port>]:<command>
573// * <prefix>:<serial>:<command>
574// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
575//
576// The returned pointer will point to the ':' just before <command>, or nullptr if not found.
Dan Austinb4cff492016-03-28 15:32:37 -0700577char* skip_host_serial(char* service) {
David Pursell3f902aa2016-03-01 08:58:26 -0800578 static const std::vector<std::string>& prefixes =
579 *(new std::vector<std::string>{"usb:", "product:", "model:", "device:"});
Terence Haddock28e13902011-03-16 09:43:56 +0100580
David Pursell3f902aa2016-03-01 08:58:26 -0800581 for (const std::string& prefix : prefixes) {
582 if (!strncmp(service, prefix.c_str(), prefix.length())) {
583 return strchr(service + prefix.length(), ':');
584 }
Scott Anderson3608d832012-05-31 12:04:23 -0700585 }
586
David Pursell3f902aa2016-03-01 08:58:26 -0800587 // For fastboot compatibility, ignore protocol prefixes.
588 if (!strncmp(service, "tcp:", 4) || !strncmp(service, "udp:", 4)) {
589 service += 4;
590 }
591
David Pursell73d55aa2016-09-21 12:08:37 -0700592 // Check for an IPv6 address. `adb connect` creates the serial number from the canonical
593 // network address so it will always have the [] delimiters.
594 if (service[0] == '[') {
595 char* ipv6_end = strchr(service, ']');
596 if (ipv6_end != nullptr) {
597 service = ipv6_end;
598 }
599 }
600
601 // The next colon we find must either begin the port field or the command field.
602 char* colon_ptr = strchr(service, ':');
603 if (!colon_ptr) {
David Pursell3f902aa2016-03-01 08:58:26 -0800604 // No colon in service string.
605 return nullptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100606 }
David Pursell3f902aa2016-03-01 08:58:26 -0800607
David Pursell73d55aa2016-09-21 12:08:37 -0700608 // If the next field is only decimal digits and ends with another colon, it's a port.
609 char* serial_end = colon_ptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100610 if (isdigit(serial_end[1])) {
611 serial_end++;
David Pursell3f902aa2016-03-01 08:58:26 -0800612 while (*serial_end && isdigit(*serial_end)) {
Terence Haddock28e13902011-03-16 09:43:56 +0100613 serial_end++;
614 }
David Pursell3f902aa2016-03-01 08:58:26 -0800615 if (*serial_end != ':') {
David Pursell73d55aa2016-09-21 12:08:37 -0700616 // Something other than "<port>:" was found, this must be the command field instead.
617 serial_end = colon_ptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100618 }
619 }
620 return serial_end;
621}
622
David Pursell3f902aa2016-03-01 08:58:26 -0800623} // namespace internal
624
Josh Gao52bd8522016-05-17 16:55:06 -0700625#endif // ADB_HOST
Elliott Hughese67f1f82015-04-30 17:32:03 -0700626
Josh Gao27cb7dc2018-02-01 13:17:50 -0800627static int smart_socket_enqueue(asocket* s, std::string data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628#if ADB_HOST
Josh Gao52bd8522016-05-17 16:55:06 -0700629 char* service = nullptr;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700630 char* serial = nullptr;
Josh Gaob122b172017-08-16 16:57:01 -0700631 TransportId transport_id = 0;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700632 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633#endif
634
Josh Gao27cb7dc2018-02-01 13:17:50 -0800635 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636
Josh Gao27cb7dc2018-02-01 13:17:50 -0800637 if (s->smart_socket_data.empty()) {
638 s->smart_socket_data = std::move(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639 } else {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800640 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641 }
642
Josh Gao7e6683c2016-01-15 14:35:54 -0800643 /* don't bother if we can't decode the length */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800644 if (s->smart_socket_data.size() < 4) {
Josh Gao52bd8522016-05-17 16:55:06 -0700645 return 0;
646 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800647
Josh Gao27cb7dc2018-02-01 13:17:50 -0800648 uint32_t len = unhex(s->smart_socket_data.data(), 4);
649 if (len == 0 || len > MAX_PAYLOAD) {
650 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651 goto fail;
652 }
653
Josh Gao27cb7dc2018-02-01 13:17:50 -0800654 D("SS(%d): len is %u", s->id, len);
Josh Gao7e6683c2016-01-15 14:35:54 -0800655 /* can't do anything until we have the full header */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800656 if ((len + 4) > s->smart_socket_data.size()) {
657 D("SS(%d): waiting for %zu more bytes", s->id, len + 4 - s->smart_socket_data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658 return 0;
659 }
660
Josh Gao27cb7dc2018-02-01 13:17:50 -0800661 s->smart_socket_data[len + 4] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662
Josh Gao27cb7dc2018-02-01 13:17:50 -0800663 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800664
665#if ADB_HOST
Josh Gao27cb7dc2018-02-01 13:17:50 -0800666 service = &s->smart_socket_data[4];
Josh Gao52bd8522016-05-17 16:55:06 -0700667 if (!strncmp(service, "host-serial:", strlen("host-serial:"))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 char* serial_end;
669 service += strlen("host-serial:");
670
Terence Haddock28e13902011-03-16 09:43:56 +0100671 // serial number should follow "host:" and could be a host:port string.
David Pursell3f902aa2016-03-01 08:58:26 -0800672 serial_end = internal::skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 if (serial_end) {
Josh Gao52bd8522016-05-17 16:55:06 -0700674 *serial_end = 0; // terminate string
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675 serial = service;
676 service = serial_end + 1;
677 }
Josh Gaob122b172017-08-16 16:57:01 -0700678 } else if (!strncmp(service, "host-transport-id:", strlen("host-transport-id:"))) {
679 service += strlen("host-transport-id:");
680 transport_id = strtoll(service, &service, 10);
681
682 if (*service != ':') {
683 return -1;
684 }
685 service++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700687 type = kTransportUsb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 service += strlen("host-usb:");
689 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700690 type = kTransportLocal;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 service += strlen("host-local:");
692 } else if (!strncmp(service, "host:", strlen("host:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700693 type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 service += strlen("host:");
695 } else {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700696 service = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 }
698
699 if (service) {
Josh Gao52bd8522016-05-17 16:55:06 -0700700 asocket* s2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701
Josh Gao52bd8522016-05-17 16:55:06 -0700702 /* some requests are handled immediately -- in that
703 ** case the handle_host_request() routine has sent
704 ** the OKAY or FAIL message and all we have to do
705 ** is clean up.
706 */
Josh Gaob122b172017-08-16 16:57:01 -0700707 if (handle_host_request(service, type, serial, transport_id, s->peer->fd, s) == 0) {
Josh Gao52bd8522016-05-17 16:55:06 -0700708 /* XXX fail message? */
709 D("SS(%d): handled host service '%s'", s->id, service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 goto fail;
711 }
712 if (!strncmp(service, "transport", strlen("transport"))) {
Josh Gao52bd8522016-05-17 16:55:06 -0700713 D("SS(%d): okay transport", s->id);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800714 s->smart_socket_data.clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715 return 0;
716 }
717
Josh Gao52bd8522016-05-17 16:55:06 -0700718 /* try to find a local service with this name.
719 ** if no such service exists, we'll fail out
720 ** and tear down here.
721 */
Josh Gaob122b172017-08-16 16:57:01 -0700722 s2 = create_host_service_socket(service, serial, transport_id);
Josh Gao52bd8522016-05-17 16:55:06 -0700723 if (s2 == 0) {
724 D("SS(%d): couldn't create host service '%s'", s->id, service);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700725 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726 goto fail;
727 }
728
Josh Gao52bd8522016-05-17 16:55:06 -0700729 /* we've connected to a local host service,
730 ** so we make our peer back into a regular
731 ** local socket and bind it to the new local
732 ** service socket, acknowledge the successful
733 ** connection, and close this smart socket now
734 ** that its work is done.
735 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700736 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737
738 s->peer->ready = local_socket_ready;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700739 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 s->peer->close = local_socket_close;
741 s->peer->peer = s2;
742 s2->peer = s->peer;
743 s->peer = 0;
Josh Gao52bd8522016-05-17 16:55:06 -0700744 D("SS(%d): okay", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800745 s->close(s);
746
Josh Gao52bd8522016-05-17 16:55:06 -0700747 /* initial state is "ready" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 s2->ready(s2);
749 return 0;
750 }
751#else /* !ADB_HOST */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700752 if (s->transport == nullptr) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700753 std::string error_msg = "unknown failure";
Josh Gaob122b172017-08-16 16:57:01 -0700754 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700755 if (s->transport == nullptr) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700756 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800757 goto fail;
758 }
759 }
760#endif
761
Josh Gao22d2b3e2016-10-27 14:01:08 -0700762 if (!s->transport) {
763 SendFail(s->peer->fd, "device offline (no transport)");
764 goto fail;
Yabin Cuib5e11412017-03-10 16:01:01 -0800765 } else if (s->transport->GetConnectionState() == kCsOffline) {
Josh Gao52bd8522016-05-17 16:55:06 -0700766 /* if there's no remote we fail the connection
767 ** right here and terminate it
768 */
Josh Gao22d2b3e2016-10-27 14:01:08 -0700769 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800770 goto fail;
771 }
772
Josh Gao52bd8522016-05-17 16:55:06 -0700773 /* instrument our peer to pass the success or fail
774 ** message back once it connects or closes, then
775 ** detach from it, request the connection, and
776 ** tear down
777 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 s->peer->ready = local_socket_ready_notify;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700779 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780 s->peer->close = local_socket_close_notify;
781 s->peer->peer = 0;
Josh Gao52bd8522016-05-17 16:55:06 -0700782 /* give him our transport and upref it */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800783 s->peer->transport = s->transport;
784
Josh Gao27cb7dc2018-02-01 13:17:50 -0800785 connect_to_remote(s->peer, s->smart_socket_data.data() + 4);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786 s->peer = 0;
787 s->close(s);
788 return 1;
789
790fail:
Josh Gao52bd8522016-05-17 16:55:06 -0700791 /* we're going to close our peer as a side-effect, so
792 ** return -1 to signal that state to the local socket
793 ** who is enqueueing against us
794 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800795 s->close(s);
796 return -1;
797}
798
Josh Gao52bd8522016-05-17 16:55:06 -0700799static void smart_socket_ready(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700800 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801}
802
Josh Gao52bd8522016-05-17 16:55:06 -0700803static void smart_socket_close(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700804 D("SS(%d): closed", s->id);
Josh Gao52bd8522016-05-17 16:55:06 -0700805 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806 s->peer->peer = 0;
807 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500808 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 }
Josh Gaoe0361d12018-02-12 17:24:00 -0800810 delete s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811}
812
Josh Gao52bd8522016-05-17 16:55:06 -0700813static asocket* create_smart_socket(void) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700814 D("Creating smart socket");
Josh Gaoe0361d12018-02-12 17:24:00 -0800815 asocket* s = new asocket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800816 s->enqueue = smart_socket_enqueue;
817 s->ready = smart_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100818 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800819 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800820
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700821 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822 return s;
823}
824
Josh Gao52bd8522016-05-17 16:55:06 -0700825void connect_to_smartsocket(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700826 D("Connecting to smart socket");
Josh Gao52bd8522016-05-17 16:55:06 -0700827 asocket* ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800828 s->peer = ss;
829 ss->peer = s;
830 s->ready(s);
831}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100832
833size_t asocket::get_max_payload() const {
834 size_t max_payload = MAX_PAYLOAD;
835 if (transport) {
836 max_payload = std::min(max_payload, transport->get_max_payload());
837 }
838 if (peer && peer->transport) {
839 max_payload = std::min(max_payload, peer->transport->get_max_payload());
840 }
841 return max_payload;
842}