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