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