blob: e9c45b78d98831cf75c98ddc4da1e8dff6b05c55 [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);
173 free(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) {
350 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
351 if (s == NULL) {
352 fatal("cannot allocate socket");
353 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 s->fd = fd;
355 s->enqueue = local_socket_enqueue;
356 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100357 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 s->close = local_socket_close;
JP Abgrall408fa572011-03-16 15:57:42 -0700359 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360
361 fdevent_install(&s->fde, fd, local_socket_event_func, s);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700362 D("LS(%d): created (fd=%d)", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 return s;
364}
365
Josh Gao52bd8522016-05-17 16:55:06 -0700366asocket* create_local_service_socket(const char* name, const atransport* transport) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367#if !ADB_HOST
Josh Gao52bd8522016-05-17 16:55:06 -0700368 if (!strcmp(name, "jdwp")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369 return create_jdwp_service_socket();
370 }
Josh Gao52bd8522016-05-17 16:55:06 -0700371 if (!strcmp(name, "track-jdwp")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 return create_jdwp_tracker_service_socket();
373 }
374#endif
David Pursell0955c662015-08-31 10:42:13 -0700375 int fd = service_to_fd(name, transport);
Josh Gao52bd8522016-05-17 16:55:06 -0700376 if (fd < 0) {
Elliott Hughesffc73a32016-06-15 14:46:56 -0700377 return nullptr;
Josh Gao52bd8522016-05-17 16:55:06 -0700378 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379
Dan Pasanen98858812014-10-06 12:57:20 -0500380 asocket* s = create_local_socket(fd);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700381 D("LS(%d): bound to '%s' via %d", s->id, name, fd);
Benoit Gobyf366b362012-03-16 14:50:07 -0700382
JP Abgrallf91259a2012-03-30 13:19:11 -0700383#if !ADB_HOST
Mark Salyzyn97787a02016-03-28 15:52:13 -0700384 if ((!strncmp(name, "root:", 5) && getuid() != 0 && __android_log_is_debuggable()) ||
Josh Gao52bd8522016-05-17 16:55:06 -0700385 (!strncmp(name, "unroot:", 7) && getuid() == 0) ||
386 !strncmp(name, "usb:", 4) ||
387 !strncmp(name, "tcpip:", 6)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700388 D("LS(%d): enabling exit_on_close", s->id);
Benoit Gobyf366b362012-03-16 14:50:07 -0700389 s->exit_on_close = 1;
390 }
JP Abgrallf91259a2012-03-30 13:19:11 -0700391#endif
Benoit Gobyf366b362012-03-16 14:50:07 -0700392
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 return s;
394}
395
396#if ADB_HOST
Josh Gaob122b172017-08-16 16:57:01 -0700397static asocket* create_host_service_socket(const char* name, const char* serial,
398 TransportId transport_id) {
Josh Gao52bd8522016-05-17 16:55:06 -0700399 asocket* s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400
Josh Gaob122b172017-08-16 16:57:01 -0700401 s = host_service_to_socket(name, serial, transport_id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402
403 if (s != NULL) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700404 D("LS(%d) bound to '%s'", s->id, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 return s;
406 }
407
408 return s;
409}
410#endif /* ADB_HOST */
411
Josh Gao27cb7dc2018-02-01 13:17:50 -0800412static int remote_socket_enqueue(asocket* s, std::string data) {
Josh Gao52bd8522016-05-17 16:55:06 -0700413 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 -0800414 apacket* p = get_apacket();
415
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 p->msg.command = A_WRTE;
417 p->msg.arg0 = s->peer->id;
418 p->msg.arg1 = s->id;
Josh Gao27cb7dc2018-02-01 13:17:50 -0800419 p->msg.data_length = data.size();
420
421 if (data.size() > sizeof(p->data)) {
422 put_apacket(p);
423 return -1;
424 }
425
426 // TODO: Convert apacket::data to a type that we can move into.
427 memcpy(p->data, data.data(), data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 send_packet(p, s->transport);
429 return 1;
430}
431
Josh Gao52bd8522016-05-17 16:55:06 -0700432static void remote_socket_ready(asocket* s) {
433 D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d", s->id, s->fd, s->peer->fd);
434 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 p->msg.command = A_OKAY;
436 p->msg.arg0 = s->peer->id;
437 p->msg.arg1 = s->id;
438 send_packet(p, s->transport);
439}
440
Josh Gao52bd8522016-05-17 16:55:06 -0700441static void remote_socket_shutdown(asocket* s) {
442 D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
443 s->peer ? s->peer->fd : -1);
444 apacket* p = get_apacket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445 p->msg.command = A_CLSE;
Josh Gao52bd8522016-05-17 16:55:06 -0700446 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 p->msg.arg0 = s->peer->id;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100448 }
449 p->msg.arg1 = s->id;
450 send_packet(p, s->transport);
451}
452
Josh Gao52bd8522016-05-17 16:55:06 -0700453static void remote_socket_close(asocket* s) {
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100454 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 s->peer->peer = 0;
Josh Gao52bd8522016-05-17 16:55:06 -0700456 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 -0800457 s->peer->close(s->peer);
458 }
Josh Gao52bd8522016-05-17 16:55:06 -0700459 D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d", s->id, s->fd,
460 s->peer ? s->peer->fd : -1);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700461 D("RS(%d): closed", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 free(s);
463}
464
Yabin Cuifd28f322015-08-27 18:50:04 -0700465// Create a remote socket to exchange packets with a remote service through transport
466// |t|. Where |id| is the socket id of the corresponding service on the other
467// side of the transport (it is allocated by the remote side and _cannot_ be 0).
468// Returns a new non-NULL asocket handle.
Josh Gao52bd8522016-05-17 16:55:06 -0700469asocket* create_remote_socket(unsigned id, atransport* t) {
470 if (id == 0) {
471 fatal("invalid remote socket id (0)");
472 }
Yabin Cuifd28f322015-08-27 18:50:04 -0700473 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474
Josh Gao52bd8522016-05-17 16:55:06 -0700475 if (s == NULL) {
476 fatal("cannot allocate socket");
477 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 s->id = id;
479 s->enqueue = remote_socket_enqueue;
480 s->ready = remote_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100481 s->shutdown = remote_socket_shutdown;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 s->close = remote_socket_close;
483 s->transport = t;
484
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700485 D("RS(%d): created", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 return s;
487}
488
Josh Gao52bd8522016-05-17 16:55:06 -0700489void connect_to_remote(asocket* s, const char* destination) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700490 D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
Josh Gao52bd8522016-05-17 16:55:06 -0700491 apacket* p = get_apacket();
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100492 size_t len = strlen(destination) + 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493
Josh Gao52bd8522016-05-17 16:55:06 -0700494 if (len > (s->get_max_payload() - 1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 fatal("destination oversized");
496 }
497
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700498 D("LS(%d): connect('%s')", s->id, destination);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 p->msg.command = A_OPEN;
500 p->msg.arg0 = s->id;
501 p->msg.data_length = len;
Josh Gao52bd8522016-05-17 16:55:06 -0700502 strcpy((char*)p->data, destination);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 send_packet(p, s->transport);
504}
505
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506/* this is used by magic sockets to rig local sockets to
507 send the go-ahead message when they connect */
Josh Gao52bd8522016-05-17 16:55:06 -0700508static void local_socket_ready_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100510 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700512 SendOkay(s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 s->ready(s);
514}
515
516/* this is used by magic sockets to rig local sockets to
517 send the failure message if they are closed before
518 connected (to avoid closing them without a status message) */
Josh Gao52bd8522016-05-17 16:55:06 -0700519static void local_socket_close_notify(asocket* s) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 s->ready = local_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100521 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 s->close = local_socket_close;
Elliott Hughese67f1f82015-04-30 17:32:03 -0700523 SendFail(s->fd, "closed");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 s->close(s);
525}
526
Josh Gao27cb7dc2018-02-01 13:17:50 -0800527static unsigned unhex(const char* s, int len) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 unsigned n = 0, c;
529
Josh Gao52bd8522016-05-17 16:55:06 -0700530 while (len-- > 0) {
531 switch ((c = *s++)) {
532 case '0':
533 case '1':
534 case '2':
535 case '3':
536 case '4':
537 case '5':
538 case '6':
539 case '7':
540 case '8':
541 case '9':
542 c -= '0';
543 break;
544 case 'a':
545 case 'b':
546 case 'c':
547 case 'd':
548 case 'e':
549 case 'f':
550 c = c - 'a' + 10;
551 break;
552 case 'A':
553 case 'B':
554 case 'C':
555 case 'D':
556 case 'E':
557 case 'F':
558 c = c - 'A' + 10;
559 break;
560 default:
561 return 0xffffffff;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800562 }
563
564 n = (n << 4) | c;
565 }
566
567 return n;
568}
569
Elliott Hughese67f1f82015-04-30 17:32:03 -0700570#if ADB_HOST
571
David Pursell3f902aa2016-03-01 08:58:26 -0800572namespace internal {
Scott Anderson2ca3e6b2012-05-30 18:11:27 -0700573
David Pursell3f902aa2016-03-01 08:58:26 -0800574// Returns the position in |service| following the target serial parameter. Serial format can be
575// any of:
576// * [tcp:|udp:]<serial>[:<port>]:<command>
577// * <prefix>:<serial>:<command>
578// Where <port> must be a base-10 number and <prefix> may be any of {usb,product,model,device}.
579//
580// The returned pointer will point to the ':' just before <command>, or nullptr if not found.
Dan Austinb4cff492016-03-28 15:32:37 -0700581char* skip_host_serial(char* service) {
David Pursell3f902aa2016-03-01 08:58:26 -0800582 static const std::vector<std::string>& prefixes =
583 *(new std::vector<std::string>{"usb:", "product:", "model:", "device:"});
Terence Haddock28e13902011-03-16 09:43:56 +0100584
David Pursell3f902aa2016-03-01 08:58:26 -0800585 for (const std::string& prefix : prefixes) {
586 if (!strncmp(service, prefix.c_str(), prefix.length())) {
587 return strchr(service + prefix.length(), ':');
588 }
Scott Anderson3608d832012-05-31 12:04:23 -0700589 }
590
David Pursell3f902aa2016-03-01 08:58:26 -0800591 // For fastboot compatibility, ignore protocol prefixes.
592 if (!strncmp(service, "tcp:", 4) || !strncmp(service, "udp:", 4)) {
593 service += 4;
594 }
595
David Pursell73d55aa2016-09-21 12:08:37 -0700596 // Check for an IPv6 address. `adb connect` creates the serial number from the canonical
597 // network address so it will always have the [] delimiters.
598 if (service[0] == '[') {
599 char* ipv6_end = strchr(service, ']');
600 if (ipv6_end != nullptr) {
601 service = ipv6_end;
602 }
603 }
604
605 // The next colon we find must either begin the port field or the command field.
606 char* colon_ptr = strchr(service, ':');
607 if (!colon_ptr) {
David Pursell3f902aa2016-03-01 08:58:26 -0800608 // No colon in service string.
609 return nullptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100610 }
David Pursell3f902aa2016-03-01 08:58:26 -0800611
David Pursell73d55aa2016-09-21 12:08:37 -0700612 // If the next field is only decimal digits and ends with another colon, it's a port.
613 char* serial_end = colon_ptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100614 if (isdigit(serial_end[1])) {
615 serial_end++;
David Pursell3f902aa2016-03-01 08:58:26 -0800616 while (*serial_end && isdigit(*serial_end)) {
Terence Haddock28e13902011-03-16 09:43:56 +0100617 serial_end++;
618 }
David Pursell3f902aa2016-03-01 08:58:26 -0800619 if (*serial_end != ':') {
David Pursell73d55aa2016-09-21 12:08:37 -0700620 // Something other than "<port>:" was found, this must be the command field instead.
621 serial_end = colon_ptr;
Terence Haddock28e13902011-03-16 09:43:56 +0100622 }
623 }
624 return serial_end;
625}
626
David Pursell3f902aa2016-03-01 08:58:26 -0800627} // namespace internal
628
Josh Gao52bd8522016-05-17 16:55:06 -0700629#endif // ADB_HOST
Elliott Hughese67f1f82015-04-30 17:32:03 -0700630
Josh Gao27cb7dc2018-02-01 13:17:50 -0800631static int smart_socket_enqueue(asocket* s, std::string data) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632#if ADB_HOST
Josh Gao52bd8522016-05-17 16:55:06 -0700633 char* service = nullptr;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700634 char* serial = nullptr;
Josh Gaob122b172017-08-16 16:57:01 -0700635 TransportId transport_id = 0;
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700636 TransportType type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800637#endif
638
Josh Gao27cb7dc2018-02-01 13:17:50 -0800639 D("SS(%d): enqueue %zu", s->id, data.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800640
Josh Gao27cb7dc2018-02-01 13:17:50 -0800641 if (s->smart_socket_data.empty()) {
642 s->smart_socket_data = std::move(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800643 } else {
Josh Gao27cb7dc2018-02-01 13:17:50 -0800644 std::copy(data.begin(), data.end(), std::back_inserter(s->smart_socket_data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800645 }
646
Josh Gao7e6683c2016-01-15 14:35:54 -0800647 /* don't bother if we can't decode the length */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800648 if (s->smart_socket_data.size() < 4) {
Josh Gao52bd8522016-05-17 16:55:06 -0700649 return 0;
650 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651
Josh Gao27cb7dc2018-02-01 13:17:50 -0800652 uint32_t len = unhex(s->smart_socket_data.data(), 4);
653 if (len == 0 || len > MAX_PAYLOAD) {
654 D("SS(%d): bad size (%u)", s->id, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655 goto fail;
656 }
657
Josh Gao27cb7dc2018-02-01 13:17:50 -0800658 D("SS(%d): len is %u", s->id, len);
Josh Gao7e6683c2016-01-15 14:35:54 -0800659 /* can't do anything until we have the full header */
Josh Gao27cb7dc2018-02-01 13:17:50 -0800660 if ((len + 4) > s->smart_socket_data.size()) {
661 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 -0800662 return 0;
663 }
664
Josh Gao27cb7dc2018-02-01 13:17:50 -0800665 s->smart_socket_data[len + 4] = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666
Josh Gao27cb7dc2018-02-01 13:17:50 -0800667 D("SS(%d): '%s'", s->id, (char*)(s->smart_socket_data.data() + 4));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668
669#if ADB_HOST
Josh Gao27cb7dc2018-02-01 13:17:50 -0800670 service = &s->smart_socket_data[4];
Josh Gao52bd8522016-05-17 16:55:06 -0700671 if (!strncmp(service, "host-serial:", strlen("host-serial:"))) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672 char* serial_end;
673 service += strlen("host-serial:");
674
Terence Haddock28e13902011-03-16 09:43:56 +0100675 // serial number should follow "host:" and could be a host:port string.
David Pursell3f902aa2016-03-01 08:58:26 -0800676 serial_end = internal::skip_host_serial(service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 if (serial_end) {
Josh Gao52bd8522016-05-17 16:55:06 -0700678 *serial_end = 0; // terminate string
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 serial = service;
680 service = serial_end + 1;
681 }
Josh Gaob122b172017-08-16 16:57:01 -0700682 } else if (!strncmp(service, "host-transport-id:", strlen("host-transport-id:"))) {
683 service += strlen("host-transport-id:");
684 transport_id = strtoll(service, &service, 10);
685
686 if (*service != ':') {
687 return -1;
688 }
689 service++;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700691 type = kTransportUsb;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 service += strlen("host-usb:");
693 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700694 type = kTransportLocal;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 service += strlen("host-local:");
696 } else if (!strncmp(service, "host:", strlen("host:"))) {
Elliott Hughes3bd73c12015-05-05 13:10:43 -0700697 type = kTransportAny;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 service += strlen("host:");
699 } else {
Elliott Hughes8d28e192015-10-07 14:55:10 -0700700 service = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 }
702
703 if (service) {
Josh Gao52bd8522016-05-17 16:55:06 -0700704 asocket* s2;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705
Josh Gao52bd8522016-05-17 16:55:06 -0700706 /* some requests are handled immediately -- in that
707 ** case the handle_host_request() routine has sent
708 ** the OKAY or FAIL message and all we have to do
709 ** is clean up.
710 */
Josh Gaob122b172017-08-16 16:57:01 -0700711 if (handle_host_request(service, type, serial, transport_id, s->peer->fd, s) == 0) {
Josh Gao52bd8522016-05-17 16:55:06 -0700712 /* XXX fail message? */
713 D("SS(%d): handled host service '%s'", s->id, service);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714 goto fail;
715 }
716 if (!strncmp(service, "transport", strlen("transport"))) {
Josh Gao52bd8522016-05-17 16:55:06 -0700717 D("SS(%d): okay transport", s->id);
Josh Gao27cb7dc2018-02-01 13:17:50 -0800718 s->smart_socket_data.clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 return 0;
720 }
721
Josh Gao52bd8522016-05-17 16:55:06 -0700722 /* try to find a local service with this name.
723 ** if no such service exists, we'll fail out
724 ** and tear down here.
725 */
Josh Gaob122b172017-08-16 16:57:01 -0700726 s2 = create_host_service_socket(service, serial, transport_id);
Josh Gao52bd8522016-05-17 16:55:06 -0700727 if (s2 == 0) {
728 D("SS(%d): couldn't create host service '%s'", s->id, service);
Elliott Hughese67f1f82015-04-30 17:32:03 -0700729 SendFail(s->peer->fd, "unknown host service");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730 goto fail;
731 }
732
Josh Gao52bd8522016-05-17 16:55:06 -0700733 /* we've connected to a local host service,
734 ** so we make our peer back into a regular
735 ** local socket and bind it to the new local
736 ** service socket, acknowledge the successful
737 ** connection, and close this smart socket now
738 ** that its work is done.
739 */
Elliott Hughese67f1f82015-04-30 17:32:03 -0700740 SendOkay(s->peer->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741
742 s->peer->ready = local_socket_ready;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700743 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744 s->peer->close = local_socket_close;
745 s->peer->peer = s2;
746 s2->peer = s->peer;
747 s->peer = 0;
Josh Gao52bd8522016-05-17 16:55:06 -0700748 D("SS(%d): okay", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800749 s->close(s);
750
Josh Gao52bd8522016-05-17 16:55:06 -0700751 /* initial state is "ready" */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752 s2->ready(s2);
753 return 0;
754 }
755#else /* !ADB_HOST */
Elliott Hughes8d28e192015-10-07 14:55:10 -0700756 if (s->transport == nullptr) {
Elliott Hughes7be29c82015-04-16 22:54:44 -0700757 std::string error_msg = "unknown failure";
Josh Gaob122b172017-08-16 16:57:01 -0700758 s->transport = acquire_one_transport(kTransportAny, nullptr, 0, nullptr, &error_msg);
Elliott Hughes8d28e192015-10-07 14:55:10 -0700759 if (s->transport == nullptr) {
Elliott Hughese67f1f82015-04-30 17:32:03 -0700760 SendFail(s->peer->fd, error_msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800761 goto fail;
762 }
763 }
764#endif
765
Josh Gao22d2b3e2016-10-27 14:01:08 -0700766 if (!s->transport) {
767 SendFail(s->peer->fd, "device offline (no transport)");
768 goto fail;
Yabin Cuib5e11412017-03-10 16:01:01 -0800769 } else if (s->transport->GetConnectionState() == kCsOffline) {
Josh Gao52bd8522016-05-17 16:55:06 -0700770 /* if there's no remote we fail the connection
771 ** right here and terminate it
772 */
Josh Gao22d2b3e2016-10-27 14:01:08 -0700773 SendFail(s->peer->fd, "device offline (transport offline)");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800774 goto fail;
775 }
776
Josh Gao52bd8522016-05-17 16:55:06 -0700777 /* instrument our peer to pass the success or fail
778 ** message back once it connects or closes, then
779 ** detach from it, request the connection, and
780 ** tear down
781 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782 s->peer->ready = local_socket_ready_notify;
Elliott Hughes8d28e192015-10-07 14:55:10 -0700783 s->peer->shutdown = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800784 s->peer->close = local_socket_close_notify;
785 s->peer->peer = 0;
Josh Gao52bd8522016-05-17 16:55:06 -0700786 /* give him our transport and upref it */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787 s->peer->transport = s->transport;
788
Josh Gao27cb7dc2018-02-01 13:17:50 -0800789 connect_to_remote(s->peer, s->smart_socket_data.data() + 4);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790 s->peer = 0;
791 s->close(s);
792 return 1;
793
794fail:
Josh Gao52bd8522016-05-17 16:55:06 -0700795 /* we're going to close our peer as a side-effect, so
796 ** return -1 to signal that state to the local socket
797 ** who is enqueueing against us
798 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800799 s->close(s);
800 return -1;
801}
802
Josh Gao52bd8522016-05-17 16:55:06 -0700803static void smart_socket_ready(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700804 D("SS(%d): ready", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805}
806
Josh Gao52bd8522016-05-17 16:55:06 -0700807static void smart_socket_close(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700808 D("SS(%d): closed", s->id);
Josh Gao52bd8522016-05-17 16:55:06 -0700809 if (s->peer) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800810 s->peer->peer = 0;
811 s->peer->close(s->peer);
Tom Marlin49f18572011-05-13 13:24:55 -0500812 s->peer = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800813 }
814 free(s);
815}
816
Josh Gao52bd8522016-05-17 16:55:06 -0700817static asocket* create_smart_socket(void) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700818 D("Creating smart socket");
Josh Gao52bd8522016-05-17 16:55:06 -0700819 asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300820 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800821 s->enqueue = smart_socket_enqueue;
822 s->ready = smart_socket_ready;
David 'Digit' Turner818d6412013-12-13 14:09:44 +0100823 s->shutdown = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824 s->close = smart_socket_close;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800825
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700826 D("SS(%d)", s->id);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 return s;
828}
829
Josh Gao52bd8522016-05-17 16:55:06 -0700830void connect_to_smartsocket(asocket* s) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700831 D("Connecting to smart socket");
Josh Gao52bd8522016-05-17 16:55:06 -0700832 asocket* ss = create_smart_socket();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800833 s->peer = ss;
834 ss->peer = s;
835 s->ready(s);
836}
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100837
838size_t asocket::get_max_payload() const {
839 size_t max_payload = MAX_PAYLOAD;
840 if (transport) {
841 max_payload = std::min(max_payload, transport->get_max_payload());
842 }
843 if (peer && peer->transport) {
844 max_payload = std::min(max_payload, peer->transport->get_max_payload());
845 }
846 return max_payload;
847}