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