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