The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 17 | #define TRACE_TAG TRACE_SOCKETS |
| 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 21 | #include <ctype.h> |
| 22 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #include <string.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 28 | #include <algorithm> |
| 29 | #include <mutex> |
| 30 | #include <string> |
| 31 | #include <vector> |
| 32 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 33 | #if !ADB_HOST |
| 34 | #include "cutils/properties.h" |
| 35 | #endif |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 36 | |
| 37 | #include "adb.h" |
| 38 | #include "adb_io.h" |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 39 | #include "sysdeps/mutex.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 40 | #include "transport.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 42 | #if !defined(__BIONIC__) |
| 43 | using std::recursive_mutex; |
| 44 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 46 | static void local_socket_close(asocket* s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 48 | static recursive_mutex& local_socket_list_lock = *new recursive_mutex(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | static unsigned local_socket_next_id = 1; |
| 50 | |
| 51 | static asocket local_socket_list = { |
| 52 | .next = &local_socket_list, |
| 53 | .prev = &local_socket_list, |
| 54 | }; |
| 55 | |
| 56 | /* the the list of currently closing local sockets. |
| 57 | ** these have no peer anymore, but still packets to |
| 58 | ** write to their fd. |
| 59 | */ |
| 60 | static asocket local_socket_closing_list = { |
| 61 | .next = &local_socket_closing_list, |
| 62 | .prev = &local_socket_closing_list, |
| 63 | }; |
| 64 | |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 65 | // Parse the global list of sockets to find one with id |local_id|. |
| 66 | // If |peer_id| is not 0, also check that it is connected to a peer |
| 67 | // with id |peer_id|. Returns an asocket handle on success, NULL on failure. |
| 68 | asocket *find_local_socket(unsigned local_id, unsigned peer_id) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 | { |
| 70 | asocket *s; |
| 71 | asocket *result = NULL; |
| 72 | |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 73 | std::lock_guard<recursive_mutex> lock(local_socket_list_lock); |
André Goddard Rosa | 8182829 | 2010-06-12 11:40:20 -0300 | [diff] [blame] | 74 | for (s = local_socket_list.next; s != &local_socket_list; s = s->next) { |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 75 | if (s->id != local_id) |
| 76 | continue; |
| 77 | if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) { |
André Goddard Rosa | 8182829 | 2010-06-12 11:40:20 -0300 | [diff] [blame] | 78 | result = s; |
André Goddard Rosa | 8182829 | 2010-06-12 11:40:20 -0300 | [diff] [blame] | 79 | } |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 80 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | insert_local_socket(asocket* s, asocket* list) |
| 88 | { |
| 89 | s->next = list; |
| 90 | s->prev = s->next->prev; |
| 91 | s->prev->next = s; |
| 92 | s->next->prev = s; |
| 93 | } |
| 94 | |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 95 | void install_local_socket(asocket* s) { |
| 96 | std::lock_guard<recursive_mutex> lock(local_socket_list_lock); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | |
| 98 | s->id = local_socket_next_id++; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 99 | |
| 100 | // Socket ids should never be 0. |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 101 | if (local_socket_next_id == 0) { |
| 102 | fatal("local socket id overflow"); |
| 103 | } |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 104 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 105 | insert_local_socket(s, &local_socket_list); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void remove_socket(asocket *s) |
| 109 | { |
| 110 | // socket_list_lock should already be held |
| 111 | if (s->prev && s->next) |
| 112 | { |
| 113 | s->prev->next = s->next; |
| 114 | s->next->prev = s->prev; |
| 115 | s->next = 0; |
| 116 | s->prev = 0; |
| 117 | s->id = 0; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void close_all_sockets(atransport *t) |
| 122 | { |
| 123 | asocket *s; |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 124 | /* this is a little gross, but since s->close() *will* modify |
| 125 | ** the list out from under you, your options are limited. |
| 126 | */ |
| 127 | std::lock_guard<recursive_mutex> lock(local_socket_list_lock); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | restart: |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 129 | for (s = local_socket_list.next; s != &local_socket_list; s = s->next) { |
| 130 | if (s->transport == t || (s->peer && s->peer->transport == t)) { |
| 131 | local_socket_close(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | goto restart; |
| 133 | } |
| 134 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static int local_socket_enqueue(asocket *s, apacket *p) |
| 138 | { |
| 139 | D("LS(%d): enqueue %d\n", s->id, p->len); |
| 140 | |
| 141 | p->ptr = p->data; |
| 142 | |
| 143 | /* if there is already data queue'd, we will receive |
| 144 | ** events when it's time to write. just add this to |
| 145 | ** the tail |
| 146 | */ |
| 147 | if(s->pkt_first) { |
| 148 | goto enqueue; |
| 149 | } |
| 150 | |
| 151 | /* write as much as we can, until we |
| 152 | ** would block or there is an error/eof |
| 153 | */ |
| 154 | while(p->len > 0) { |
| 155 | int r = adb_write(s->fd, p->ptr, p->len); |
| 156 | if(r > 0) { |
| 157 | p->len -= r; |
| 158 | p->ptr += r; |
| 159 | continue; |
| 160 | } |
| 161 | if((r == 0) || (errno != EAGAIN)) { |
| 162 | D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) ); |
| 163 | s->close(s); |
| 164 | return 1; /* not ready (error) */ |
| 165 | } else { |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if(p->len == 0) { |
| 171 | put_apacket(p); |
| 172 | return 0; /* ready for more data */ |
| 173 | } |
| 174 | |
| 175 | enqueue: |
| 176 | p->next = 0; |
| 177 | if(s->pkt_first) { |
| 178 | s->pkt_last->next = p; |
| 179 | } else { |
| 180 | s->pkt_first = p; |
| 181 | } |
| 182 | s->pkt_last = p; |
| 183 | |
| 184 | /* make sure we are notified when we can drain the queue */ |
| 185 | fdevent_add(&s->fde, FDE_WRITE); |
| 186 | |
| 187 | return 1; /* not ready (backlog) */ |
| 188 | } |
| 189 | |
| 190 | static void local_socket_ready(asocket *s) |
| 191 | { |
Nanik Tolaram | b627a0e | 2015-02-18 22:53:37 +1100 | [diff] [blame] | 192 | /* far side is ready for data, pay attention to |
| 193 | readable events */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | fdevent_add(&s->fde, FDE_READ); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 195 | } |
| 196 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 197 | // be sure to hold the socket list lock when calling this |
| 198 | static void local_socket_destroy(asocket *s) |
| 199 | { |
| 200 | apacket *p, *n; |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 201 | int exit_on_close = s->exit_on_close; |
| 202 | |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 203 | D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 204 | |
| 205 | /* IMPORTANT: the remove closes the fd |
| 206 | ** that belongs to this socket |
| 207 | */ |
| 208 | fdevent_remove(&s->fde); |
| 209 | |
| 210 | /* dispose of any unwritten data */ |
| 211 | for(p = s->pkt_first; p; p = n) { |
| 212 | D("LS(%d): discarding %d bytes\n", s->id, p->len); |
| 213 | n = p->next; |
| 214 | put_apacket(p); |
| 215 | } |
| 216 | remove_socket(s); |
| 217 | free(s); |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 218 | |
| 219 | if (exit_on_close) { |
| 220 | D("local_socket_destroy: exiting\n"); |
| 221 | exit(1); |
| 222 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 225 | static void local_socket_close(asocket* s) { |
| 226 | D("entered local_socket_close. LS(%d) fd=%d", s->id, s->fd); |
| 227 | std::lock_guard<recursive_mutex> lock(local_socket_list_lock); |
| 228 | if (s->peer) { |
| 229 | D("LS(%d): closing peer. peer->id=%d peer->fd=%d", s->id, s->peer->id, s->peer->fd); |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 230 | /* Note: it's important to call shutdown before disconnecting from |
| 231 | * the peer, this ensures that remote sockets can still get the id |
| 232 | * of the local socket they're connected to, to send a CLOSE() |
| 233 | * protocol event. */ |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 234 | if (s->peer->shutdown) { |
| 235 | s->peer->shutdown(s->peer); |
Tom Marlin | 49f1857 | 2011-05-13 13:24:55 -0500 | [diff] [blame] | 236 | } |
Josh Gao | 6f641ad | 2016-05-17 19:23:39 -0700 | [diff] [blame^] | 237 | s->peer->peer = nullptr; |
| 238 | s->peer->close(s->peer); |
| 239 | s->peer = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* If we are already closing, or if there are no |
| 243 | ** pending packets, destroy immediately |
| 244 | */ |
| 245 | if (s->closing || s->pkt_first == NULL) { |
| 246 | int id = s->id; |
| 247 | local_socket_destroy(s); |
| 248 | D("LS(%d): closed\n", id); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | /* otherwise, put on the closing list |
| 253 | */ |
| 254 | D("LS(%d): closing\n", s->id); |
| 255 | s->closing = 1; |
| 256 | fdevent_del(&s->fde, FDE_READ); |
| 257 | remove_socket(s); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 258 | D("LS(%d): put on socket_closing_list fd=%d\n", s->id, s->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | insert_local_socket(s, &local_socket_closing_list); |
| 260 | } |
| 261 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 262 | static void local_socket_event_func(int fd, unsigned ev, void* _s) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 263 | { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 264 | asocket* s = reinterpret_cast<asocket*>(_s); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 265 | D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev); |
| 266 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 267 | /* put the FDE_WRITE processing before the FDE_READ |
| 268 | ** in order to simplify the code. |
| 269 | */ |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 270 | if (ev & FDE_WRITE) { |
| 271 | apacket* p; |
| 272 | while ((p = s->pkt_first) != nullptr) { |
| 273 | while (p->len > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 274 | int r = adb_write(fd, p->ptr, p->len); |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 275 | if (r == -1) { |
| 276 | /* returning here is ok because FDE_READ will |
| 277 | ** be processed in the next iteration loop |
| 278 | */ |
| 279 | if (errno == EAGAIN) { |
| 280 | return; |
| 281 | } |
| 282 | } else if (r > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 283 | p->ptr += r; |
| 284 | p->len -= r; |
| 285 | continue; |
| 286 | } |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 287 | |
Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 288 | D(" closing after write because r=%d and errno is %d\n", r, errno); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 289 | s->close(s); |
| 290 | return; |
| 291 | } |
| 292 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 293 | if (p->len == 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 294 | s->pkt_first = p->next; |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 295 | if (s->pkt_first == 0) { |
| 296 | s->pkt_last = 0; |
| 297 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 298 | put_apacket(p); |
| 299 | } |
| 300 | } |
| 301 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 302 | /* if we sent the last packet of a closing socket, |
| 303 | ** we can now destroy it. |
| 304 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 305 | if (s->closing) { |
Christopher Tate | 5b811fa | 2011-06-10 11:38:37 -0700 | [diff] [blame] | 306 | D(" closing because 'closing' is set after write\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | s->close(s); |
| 308 | return; |
| 309 | } |
| 310 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 311 | /* no more packets queued, so we can ignore |
| 312 | ** writable events again and tell our peer |
| 313 | ** to resume writing |
| 314 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 315 | fdevent_del(&s->fde, FDE_WRITE); |
| 316 | s->peer->ready(s->peer); |
| 317 | } |
| 318 | |
| 319 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 320 | if (ev & FDE_READ) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 321 | apacket *p = get_apacket(); |
| 322 | unsigned char *x = p->data; |
| 323 | size_t avail = MAX_PAYLOAD; |
| 324 | int r; |
| 325 | int is_eof = 0; |
| 326 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 327 | while (avail > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | r = adb_read(fd, x, avail); |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 329 | D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", |
| 330 | s->id, s->fd, r, r < 0 ? errno : 0, avail); |
| 331 | if (r == -1) { |
| 332 | if (errno == EAGAIN) { |
| 333 | break; |
| 334 | } |
| 335 | } else if (r > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | avail -= r; |
| 337 | x += r; |
| 338 | continue; |
| 339 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 340 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 341 | /* r = 0 or unhandled error */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 342 | is_eof = 1; |
| 343 | break; |
| 344 | } |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 345 | D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n", |
| 346 | s->id, s->fd, r, is_eof, s->fde.force_eof); |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 347 | if ((avail == MAX_PAYLOAD) || (s->peer == 0)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 | put_apacket(p); |
| 349 | } else { |
| 350 | p->len = MAX_PAYLOAD - avail; |
| 351 | |
| 352 | r = s->peer->enqueue(s->peer, p); |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 353 | D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, |
| 354 | r); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 355 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 356 | if (r < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | /* error return means they closed us as a side-effect |
| 358 | ** and we must return immediately. |
| 359 | ** |
| 360 | ** note that if we still have buffered packets, the |
| 361 | ** socket will be placed on the closing socket list. |
| 362 | ** this handler function will be called again |
| 363 | ** to process FDE_WRITE events. |
| 364 | */ |
| 365 | return; |
| 366 | } |
| 367 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 368 | if (r > 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 369 | /* if the remote cannot accept further events, |
| 370 | ** we disable notification of READs. They'll |
| 371 | ** be enabled again when we get a call to ready() |
| 372 | */ |
| 373 | fdevent_del(&s->fde, FDE_READ); |
| 374 | } |
| 375 | } |
JP Abgrall | 112445b | 2011-04-12 22:01:58 -0700 | [diff] [blame] | 376 | /* Don't allow a forced eof if data is still there */ |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 377 | if ((s->fde.force_eof && !r) || is_eof) { |
| 378 | D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", |
| 379 | is_eof, r, s->fde.force_eof); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 380 | s->close(s); |
| 381 | } |
| 382 | } |
| 383 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 384 | if (ev & FDE_ERROR){ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 385 | /* this should be caught be the next read or write |
| 386 | ** catching it here means we may skip the last few |
| 387 | ** bytes of readable data. |
| 388 | */ |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 389 | D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd); |
| 390 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 391 | return; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | asocket *create_local_socket(int fd) |
| 396 | { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 397 | asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); |
André Goddard Rosa | c419e2a | 2010-06-10 20:48:19 -0300 | [diff] [blame] | 398 | if (s == NULL) fatal("cannot allocate socket"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 399 | s->fd = fd; |
| 400 | s->enqueue = local_socket_enqueue; |
| 401 | s->ready = local_socket_ready; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 402 | s->shutdown = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 403 | s->close = local_socket_close; |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 404 | install_local_socket(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 405 | |
| 406 | fdevent_install(&s->fde, fd, local_socket_event_func, s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | D("LS(%d): created (fd=%d)\n", s->id, s->fd); |
| 408 | return s; |
| 409 | } |
| 410 | |
| 411 | asocket *create_local_service_socket(const char *name) |
| 412 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 413 | #if !ADB_HOST |
| 414 | if (!strcmp(name,"jdwp")) { |
| 415 | return create_jdwp_service_socket(); |
| 416 | } |
| 417 | if (!strcmp(name,"track-jdwp")) { |
| 418 | return create_jdwp_tracker_service_socket(); |
| 419 | } |
| 420 | #endif |
Dan Pasanen | 9885881 | 2014-10-06 12:57:20 -0500 | [diff] [blame] | 421 | int fd = service_to_fd(name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 422 | if(fd < 0) return 0; |
| 423 | |
Dan Pasanen | 9885881 | 2014-10-06 12:57:20 -0500 | [diff] [blame] | 424 | asocket* s = create_local_socket(fd); |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 425 | D("LS(%d): bound to '%s' via %d\n", s->id, name, fd); |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 426 | |
JP Abgrall | f91259a | 2012-03-30 13:19:11 -0700 | [diff] [blame] | 427 | #if !ADB_HOST |
Dan Pasanen | 9885881 | 2014-10-06 12:57:20 -0500 | [diff] [blame] | 428 | char debug[PROPERTY_VALUE_MAX]; |
jzhuan5 | 1297d22 | 2013-05-24 17:40:15 -0400 | [diff] [blame] | 429 | if (!strncmp(name, "root:", 5)) |
| 430 | property_get("ro.debuggable", debug, ""); |
| 431 | |
Dan Pasanen | 9885881 | 2014-10-06 12:57:20 -0500 | [diff] [blame] | 432 | if ((!strncmp(name, "root:", 5) && getuid() != 0 && strcmp(debug, "1") == 0) |
| 433 | || (!strncmp(name, "unroot:", 7) && getuid() == 0) |
Benoit Goby | aeceb51 | 2012-06-12 12:12:18 -0700 | [diff] [blame] | 434 | || !strncmp(name, "usb:", 4) |
| 435 | || !strncmp(name, "tcpip:", 6)) { |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 436 | D("LS(%d): enabling exit_on_close\n", s->id); |
| 437 | s->exit_on_close = 1; |
| 438 | } |
JP Abgrall | f91259a | 2012-03-30 13:19:11 -0700 | [diff] [blame] | 439 | #endif |
Benoit Goby | f366b36 | 2012-03-16 14:50:07 -0700 | [diff] [blame] | 440 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 441 | return s; |
| 442 | } |
| 443 | |
| 444 | #if ADB_HOST |
| 445 | static asocket *create_host_service_socket(const char *name, const char* serial) |
| 446 | { |
| 447 | asocket *s; |
| 448 | |
| 449 | s = host_service_to_socket(name, serial); |
| 450 | |
| 451 | if (s != NULL) { |
| 452 | D("LS(%d) bound to '%s'\n", s->id, name); |
| 453 | return s; |
| 454 | } |
| 455 | |
| 456 | return s; |
| 457 | } |
| 458 | #endif /* ADB_HOST */ |
| 459 | |
| 460 | /* a Remote socket is used to send/receive data to/from a given transport object |
| 461 | ** it needs to be closed when the transport is forcibly destroyed by the user |
| 462 | */ |
Elliott Hughes | 2d4121c | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 463 | struct aremotesocket { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 464 | asocket socket; |
| 465 | adisconnect disconnect; |
Elliott Hughes | 2d4121c | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 466 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 467 | |
| 468 | static int remote_socket_enqueue(asocket *s, apacket *p) |
| 469 | { |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 470 | D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n", |
| 471 | s->id, s->fd, s->peer->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 472 | p->msg.command = A_WRTE; |
| 473 | p->msg.arg0 = s->peer->id; |
| 474 | p->msg.arg1 = s->id; |
| 475 | p->msg.data_length = p->len; |
| 476 | send_packet(p, s->transport); |
| 477 | return 1; |
| 478 | } |
| 479 | |
| 480 | static void remote_socket_ready(asocket *s) |
| 481 | { |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 482 | D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n", |
| 483 | s->id, s->fd, s->peer->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 484 | apacket *p = get_apacket(); |
| 485 | p->msg.command = A_OKAY; |
| 486 | p->msg.arg0 = s->peer->id; |
| 487 | p->msg.arg1 = s->id; |
| 488 | send_packet(p, s->transport); |
| 489 | } |
| 490 | |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 491 | static void remote_socket_shutdown(asocket *s) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 492 | { |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 493 | D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d\n", |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 494 | s->id, s->fd, s->peer?s->peer->fd:-1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 495 | apacket *p = get_apacket(); |
| 496 | p->msg.command = A_CLSE; |
| 497 | if(s->peer) { |
| 498 | p->msg.arg0 = s->peer->id; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 499 | } |
| 500 | p->msg.arg1 = s->id; |
| 501 | send_packet(p, s->transport); |
| 502 | } |
| 503 | |
| 504 | static void remote_socket_close(asocket *s) |
| 505 | { |
| 506 | if (s->peer) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 507 | s->peer->peer = 0; |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 508 | D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n", |
| 509 | s->id, s->peer->id, s->peer->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 510 | s->peer->close(s->peer); |
| 511 | } |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 512 | D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n", |
| 513 | s->id, s->fd, s->peer?s->peer->fd:-1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 514 | D("RS(%d): closed\n", s->id); |
| 515 | remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect ); |
| 516 | free(s); |
| 517 | } |
| 518 | |
| 519 | static void remote_socket_disconnect(void* _s, atransport* t) |
| 520 | { |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 521 | asocket* s = reinterpret_cast<asocket*>(_s); |
| 522 | asocket* peer = s->peer; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 523 | |
| 524 | D("remote_socket_disconnect RS(%d)\n", s->id); |
| 525 | if (peer) { |
| 526 | peer->peer = NULL; |
| 527 | peer->close(peer); |
| 528 | } |
| 529 | remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect ); |
| 530 | free(s); |
| 531 | } |
| 532 | |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 533 | /* Create an asocket to exchange packets with a remote service through transport |
| 534 | |t|. Where |id| is the socket id of the corresponding service on the other |
| 535 | side of the transport (it is allocated by the remote side and _cannot_ be 0). |
| 536 | Returns a new non-NULL asocket handle. */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 537 | asocket *create_remote_socket(unsigned id, atransport *t) |
| 538 | { |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 539 | if (id == 0) fatal("invalid remote socket id (0)"); |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 540 | asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(aremotesocket))); |
| 541 | adisconnect* dis = &reinterpret_cast<aremotesocket*>(s)->disconnect; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 542 | |
André Goddard Rosa | c419e2a | 2010-06-10 20:48:19 -0300 | [diff] [blame] | 543 | if (s == NULL) fatal("cannot allocate socket"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 544 | s->id = id; |
| 545 | s->enqueue = remote_socket_enqueue; |
| 546 | s->ready = remote_socket_ready; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 547 | s->shutdown = remote_socket_shutdown; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | s->close = remote_socket_close; |
| 549 | s->transport = t; |
| 550 | |
| 551 | dis->func = remote_socket_disconnect; |
| 552 | dis->opaque = s; |
| 553 | add_transport_disconnect( t, dis ); |
| 554 | D("RS(%d): created\n", s->id); |
| 555 | return s; |
| 556 | } |
| 557 | |
| 558 | void connect_to_remote(asocket *s, const char *destination) |
| 559 | { |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 560 | D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 561 | apacket *p = get_apacket(); |
| 562 | int len = strlen(destination) + 1; |
| 563 | |
| 564 | if(len > (MAX_PAYLOAD-1)) { |
| 565 | fatal("destination oversized"); |
| 566 | } |
| 567 | |
| 568 | D("LS(%d): connect('%s')\n", s->id, destination); |
| 569 | p->msg.command = A_OPEN; |
| 570 | p->msg.arg0 = s->id; |
| 571 | p->msg.data_length = len; |
| 572 | strcpy((char*) p->data, destination); |
| 573 | send_packet(p, s->transport); |
| 574 | } |
| 575 | |
| 576 | |
| 577 | /* this is used by magic sockets to rig local sockets to |
| 578 | send the go-ahead message when they connect */ |
| 579 | static void local_socket_ready_notify(asocket *s) |
| 580 | { |
| 581 | s->ready = local_socket_ready; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 582 | s->shutdown = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 583 | s->close = local_socket_close; |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 584 | SendOkay(s->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 585 | s->ready(s); |
| 586 | } |
| 587 | |
| 588 | /* this is used by magic sockets to rig local sockets to |
| 589 | send the failure message if they are closed before |
| 590 | connected (to avoid closing them without a status message) */ |
| 591 | static void local_socket_close_notify(asocket *s) |
| 592 | { |
| 593 | s->ready = local_socket_ready; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 594 | s->shutdown = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 595 | s->close = local_socket_close; |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 596 | SendFail(s->fd, "closed"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 597 | s->close(s); |
| 598 | } |
| 599 | |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 600 | static unsigned unhex(unsigned char *s, int len) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 601 | { |
| 602 | unsigned n = 0, c; |
| 603 | |
| 604 | while(len-- > 0) { |
| 605 | switch((c = *s++)) { |
| 606 | case '0': case '1': case '2': |
| 607 | case '3': case '4': case '5': |
| 608 | case '6': case '7': case '8': |
| 609 | case '9': |
| 610 | c -= '0'; |
| 611 | break; |
| 612 | case 'a': case 'b': case 'c': |
| 613 | case 'd': case 'e': case 'f': |
| 614 | c = c - 'a' + 10; |
| 615 | break; |
| 616 | case 'A': case 'B': case 'C': |
| 617 | case 'D': case 'E': case 'F': |
| 618 | c = c - 'A' + 10; |
| 619 | break; |
| 620 | default: |
| 621 | return 0xffffffff; |
| 622 | } |
| 623 | |
| 624 | n = (n << 4) | c; |
| 625 | } |
| 626 | |
| 627 | return n; |
| 628 | } |
| 629 | |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 630 | #if ADB_HOST |
| 631 | |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 632 | #define PREFIX(str) { str, sizeof(str) - 1 } |
| 633 | static const struct prefix_struct { |
| 634 | const char *str; |
| 635 | const size_t len; |
| 636 | } prefixes[] = { |
| 637 | PREFIX("usb:"), |
| 638 | PREFIX("product:"), |
| 639 | PREFIX("model:"), |
| 640 | PREFIX("device:"), |
| 641 | }; |
| 642 | static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0])); |
| 643 | |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 644 | /* skip_host_serial return the position in a string |
| 645 | skipping over the 'serial' parameter in the ADB protocol, |
| 646 | where parameter string may be a host:port string containing |
| 647 | the protocol delimiter (colon). */ |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 648 | static char *skip_host_serial(char *service) { |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 649 | char *first_colon, *serial_end; |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 650 | int i; |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 651 | |
Scott Anderson | 2ca3e6b | 2012-05-30 18:11:27 -0700 | [diff] [blame] | 652 | for (i = 0; i < num_prefixes; i++) { |
| 653 | if (!strncmp(service, prefixes[i].str, prefixes[i].len)) |
| 654 | return strchr(service + prefixes[i].len, ':'); |
Scott Anderson | 3608d83 | 2012-05-31 12:04:23 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 657 | first_colon = strchr(service, ':'); |
| 658 | if (!first_colon) { |
| 659 | /* No colon in service string. */ |
| 660 | return NULL; |
| 661 | } |
| 662 | serial_end = first_colon; |
| 663 | if (isdigit(serial_end[1])) { |
| 664 | serial_end++; |
| 665 | while ((*serial_end) && isdigit(*serial_end)) { |
| 666 | serial_end++; |
| 667 | } |
| 668 | if ((*serial_end) != ':') { |
| 669 | // Something other than numbers was found, reset the end. |
| 670 | serial_end = first_colon; |
| 671 | } |
| 672 | } |
| 673 | return serial_end; |
| 674 | } |
| 675 | |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 676 | #endif // ADB_HOST |
| 677 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 678 | static int smart_socket_enqueue(asocket *s, apacket *p) |
| 679 | { |
| 680 | unsigned len; |
| 681 | #if ADB_HOST |
| 682 | char *service = NULL; |
| 683 | char* serial = NULL; |
| 684 | transport_type ttype = kTransportAny; |
| 685 | #endif |
| 686 | |
| 687 | D("SS(%d): enqueue %d\n", s->id, p->len); |
| 688 | |
| 689 | if(s->pkt_first == 0) { |
| 690 | s->pkt_first = p; |
| 691 | s->pkt_last = p; |
| 692 | } else { |
| 693 | if((s->pkt_first->len + p->len) > MAX_PAYLOAD) { |
| 694 | D("SS(%d): overflow\n", s->id); |
| 695 | put_apacket(p); |
| 696 | goto fail; |
| 697 | } |
| 698 | |
| 699 | memcpy(s->pkt_first->data + s->pkt_first->len, |
| 700 | p->data, p->len); |
| 701 | s->pkt_first->len += p->len; |
| 702 | put_apacket(p); |
| 703 | |
| 704 | p = s->pkt_first; |
| 705 | } |
| 706 | |
| 707 | /* don't bother if we can't decode the length */ |
| 708 | if(p->len < 4) return 0; |
| 709 | |
| 710 | len = unhex(p->data, 4); |
| 711 | if((len < 1) || (len > 1024)) { |
| 712 | D("SS(%d): bad size (%d)\n", s->id, len); |
| 713 | goto fail; |
| 714 | } |
| 715 | |
| 716 | D("SS(%d): len is %d\n", s->id, len ); |
| 717 | /* can't do anything until we have the full header */ |
| 718 | if((len + 4) > p->len) { |
| 719 | D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len); |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | p->data[len + 4] = 0; |
| 724 | |
| 725 | D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4)); |
| 726 | |
| 727 | #if ADB_HOST |
| 728 | service = (char *)p->data + 4; |
| 729 | if(!strncmp(service, "host-serial:", strlen("host-serial:"))) { |
| 730 | char* serial_end; |
| 731 | service += strlen("host-serial:"); |
| 732 | |
Terence Haddock | 28e1390 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 733 | // serial number should follow "host:" and could be a host:port string. |
| 734 | serial_end = skip_host_serial(service); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 735 | if (serial_end) { |
| 736 | *serial_end = 0; // terminate string |
| 737 | serial = service; |
| 738 | service = serial_end + 1; |
| 739 | } |
| 740 | } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) { |
| 741 | ttype = kTransportUsb; |
| 742 | service += strlen("host-usb:"); |
| 743 | } else if (!strncmp(service, "host-local:", strlen("host-local:"))) { |
| 744 | ttype = kTransportLocal; |
| 745 | service += strlen("host-local:"); |
| 746 | } else if (!strncmp(service, "host:", strlen("host:"))) { |
| 747 | ttype = kTransportAny; |
| 748 | service += strlen("host:"); |
| 749 | } else { |
| 750 | service = NULL; |
| 751 | } |
| 752 | |
| 753 | if (service) { |
| 754 | asocket *s2; |
| 755 | |
| 756 | /* some requests are handled immediately -- in that |
| 757 | ** case the handle_host_request() routine has sent |
| 758 | ** the OKAY or FAIL message and all we have to do |
| 759 | ** is clean up. |
| 760 | */ |
| 761 | if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) { |
| 762 | /* XXX fail message? */ |
| 763 | D( "SS(%d): handled host service '%s'\n", s->id, service ); |
| 764 | goto fail; |
| 765 | } |
| 766 | if (!strncmp(service, "transport", strlen("transport"))) { |
| 767 | D( "SS(%d): okay transport\n", s->id ); |
| 768 | p->len = 0; |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | /* try to find a local service with this name. |
| 773 | ** if no such service exists, we'll fail out |
| 774 | ** and tear down here. |
| 775 | */ |
| 776 | s2 = create_host_service_socket(service, serial); |
| 777 | if(s2 == 0) { |
| 778 | D( "SS(%d): couldn't create host service '%s'\n", s->id, service ); |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 779 | SendFail(s->peer->fd, "unknown host service"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 780 | goto fail; |
| 781 | } |
| 782 | |
| 783 | /* we've connected to a local host service, |
| 784 | ** so we make our peer back into a regular |
| 785 | ** local socket and bind it to the new local |
| 786 | ** service socket, acknowledge the successful |
| 787 | ** connection, and close this smart socket now |
| 788 | ** that its work is done. |
| 789 | */ |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 790 | SendOkay(s->peer->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 791 | |
| 792 | s->peer->ready = local_socket_ready; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 793 | s->peer->shutdown = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 794 | s->peer->close = local_socket_close; |
| 795 | s->peer->peer = s2; |
| 796 | s2->peer = s->peer; |
| 797 | s->peer = 0; |
| 798 | D( "SS(%d): okay\n", s->id ); |
| 799 | s->close(s); |
| 800 | |
| 801 | /* initial state is "ready" */ |
| 802 | s2->ready(s2); |
| 803 | return 0; |
| 804 | } |
| 805 | #else /* !ADB_HOST */ |
| 806 | if (s->transport == NULL) { |
Elliott Hughes | 7be29c8 | 2015-04-16 22:54:44 -0700 | [diff] [blame] | 807 | std::string error_msg = "unknown failure"; |
| 808 | s->transport = acquire_one_transport(CS_ANY, kTransportAny, NULL, &error_msg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 809 | |
| 810 | if (s->transport == NULL) { |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 811 | SendFail(s->peer->fd, error_msg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 812 | goto fail; |
| 813 | } |
| 814 | } |
| 815 | #endif |
| 816 | |
| 817 | if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) { |
| 818 | /* if there's no remote we fail the connection |
| 819 | ** right here and terminate it |
| 820 | */ |
Elliott Hughes | 92af733 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 821 | SendFail(s->peer->fd, "device offline (x)"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 822 | goto fail; |
| 823 | } |
| 824 | |
| 825 | |
| 826 | /* instrument our peer to pass the success or fail |
| 827 | ** message back once it connects or closes, then |
| 828 | ** detach from it, request the connection, and |
| 829 | ** tear down |
| 830 | */ |
| 831 | s->peer->ready = local_socket_ready_notify; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 832 | s->peer->shutdown = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 833 | s->peer->close = local_socket_close_notify; |
| 834 | s->peer->peer = 0; |
| 835 | /* give him our transport and upref it */ |
| 836 | s->peer->transport = s->transport; |
| 837 | |
| 838 | connect_to_remote(s->peer, (char*) (p->data + 4)); |
| 839 | s->peer = 0; |
| 840 | s->close(s); |
| 841 | return 1; |
| 842 | |
| 843 | fail: |
| 844 | /* we're going to close our peer as a side-effect, so |
| 845 | ** return -1 to signal that state to the local socket |
| 846 | ** who is enqueueing against us |
| 847 | */ |
| 848 | s->close(s); |
| 849 | return -1; |
| 850 | } |
| 851 | |
| 852 | static void smart_socket_ready(asocket *s) |
| 853 | { |
| 854 | D("SS(%d): ready\n", s->id); |
| 855 | } |
| 856 | |
| 857 | static void smart_socket_close(asocket *s) |
| 858 | { |
| 859 | D("SS(%d): closed\n", s->id); |
| 860 | if(s->pkt_first){ |
| 861 | put_apacket(s->pkt_first); |
| 862 | } |
| 863 | if(s->peer) { |
| 864 | s->peer->peer = 0; |
| 865 | s->peer->close(s->peer); |
Tom Marlin | 49f1857 | 2011-05-13 13:24:55 -0500 | [diff] [blame] | 866 | s->peer = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 867 | } |
| 868 | free(s); |
| 869 | } |
| 870 | |
Benoit Goby | 9470c2f | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 871 | static asocket *create_smart_socket(void) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 872 | { |
| 873 | D("Creating smart socket \n"); |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 874 | asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket))); |
André Goddard Rosa | c419e2a | 2010-06-10 20:48:19 -0300 | [diff] [blame] | 875 | if (s == NULL) fatal("cannot allocate socket"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 876 | s->enqueue = smart_socket_enqueue; |
| 877 | s->ready = smart_socket_ready; |
David 'Digit' Turner | 818d641 | 2013-12-13 14:09:44 +0100 | [diff] [blame] | 878 | s->shutdown = NULL; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 879 | s->close = smart_socket_close; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 880 | |
Benoit Goby | 9470c2f | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 881 | D("SS(%d)\n", s->id); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 882 | return s; |
| 883 | } |
| 884 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 885 | void connect_to_smartsocket(asocket *s) |
| 886 | { |
| 887 | D("Connecting to smart socket \n"); |
Benoit Goby | 9470c2f | 2013-02-20 15:04:53 -0800 | [diff] [blame] | 888 | asocket *ss = create_smart_socket(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 889 | s->peer = ss; |
| 890 | ss->peer = s; |
| 891 | s->ready(s); |
| 892 | } |