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