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 | |
| 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 | 8182829 | 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 | dd7bc33 | 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; |
| 202 | |
| 203 | /* IMPORTANT: the remove closes the fd |
| 204 | ** that belongs to this socket |
| 205 | */ |
| 206 | fdevent_remove(&s->fde); |
| 207 | |
| 208 | /* dispose of any unwritten data */ |
| 209 | for(p = s->pkt_first; p; p = n) { |
| 210 | D("LS(%d): discarding %d bytes\n", s->id, p->len); |
| 211 | n = p->next; |
| 212 | put_apacket(p); |
| 213 | } |
| 214 | remove_socket(s); |
| 215 | free(s); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | static void local_socket_close_locked(asocket *s) |
| 220 | { |
| 221 | if(s->peer) { |
| 222 | s->peer->peer = 0; |
| 223 | // tweak to avoid deadlock |
Tom Marlin | 49f1857 | 2011-05-13 13:24:55 -0500 | [diff] [blame^] | 224 | if (s->peer->close == local_socket_close) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 225 | local_socket_close_locked(s->peer); |
Tom Marlin | 49f1857 | 2011-05-13 13:24:55 -0500 | [diff] [blame^] | 226 | } else { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 227 | s->peer->close(s->peer); |
Tom Marlin | 49f1857 | 2011-05-13 13:24:55 -0500 | [diff] [blame^] | 228 | } |
| 229 | s->peer = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* If we are already closing, or if there are no |
| 233 | ** pending packets, destroy immediately |
| 234 | */ |
| 235 | if (s->closing || s->pkt_first == NULL) { |
| 236 | int id = s->id; |
| 237 | local_socket_destroy(s); |
| 238 | D("LS(%d): closed\n", id); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | /* otherwise, put on the closing list |
| 243 | */ |
| 244 | D("LS(%d): closing\n", s->id); |
| 245 | s->closing = 1; |
| 246 | fdevent_del(&s->fde, FDE_READ); |
| 247 | remove_socket(s); |
| 248 | insert_local_socket(s, &local_socket_closing_list); |
| 249 | } |
| 250 | |
| 251 | static void local_socket_event_func(int fd, unsigned ev, void *_s) |
| 252 | { |
| 253 | asocket *s = _s; |
| 254 | |
| 255 | /* put the FDE_WRITE processing before the FDE_READ |
| 256 | ** in order to simplify the code. |
| 257 | */ |
| 258 | if(ev & FDE_WRITE){ |
| 259 | apacket *p; |
| 260 | |
| 261 | while((p = s->pkt_first) != 0) { |
| 262 | while(p->len > 0) { |
| 263 | int r = adb_write(fd, p->ptr, p->len); |
| 264 | if(r > 0) { |
| 265 | p->ptr += r; |
| 266 | p->len -= r; |
| 267 | continue; |
| 268 | } |
| 269 | if(r < 0) { |
| 270 | /* returning here is ok because FDE_READ will |
| 271 | ** be processed in the next iteration loop |
| 272 | */ |
| 273 | if(errno == EAGAIN) return; |
| 274 | if(errno == EINTR) continue; |
| 275 | } |
| 276 | s->close(s); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | if(p->len == 0) { |
| 281 | s->pkt_first = p->next; |
| 282 | if(s->pkt_first == 0) s->pkt_last = 0; |
| 283 | put_apacket(p); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /* if we sent the last packet of a closing socket, |
| 288 | ** we can now destroy it. |
| 289 | */ |
| 290 | if (s->closing) { |
| 291 | s->close(s); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | /* no more packets queued, so we can ignore |
| 296 | ** writable events again and tell our peer |
| 297 | ** to resume writing |
| 298 | */ |
| 299 | fdevent_del(&s->fde, FDE_WRITE); |
| 300 | s->peer->ready(s->peer); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | if(ev & FDE_READ){ |
| 305 | apacket *p = get_apacket(); |
| 306 | unsigned char *x = p->data; |
| 307 | size_t avail = MAX_PAYLOAD; |
| 308 | int r; |
| 309 | int is_eof = 0; |
| 310 | |
| 311 | while(avail > 0) { |
| 312 | r = adb_read(fd, x, avail); |
| 313 | if(r > 0) { |
| 314 | avail -= r; |
| 315 | x += r; |
| 316 | continue; |
| 317 | } |
| 318 | if(r < 0) { |
| 319 | if(errno == EAGAIN) break; |
| 320 | if(errno == EINTR) continue; |
| 321 | } |
| 322 | |
| 323 | /* r = 0 or unhandled error */ |
| 324 | is_eof = 1; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | if((avail == MAX_PAYLOAD) || (s->peer == 0)) { |
| 329 | put_apacket(p); |
| 330 | } else { |
| 331 | p->len = MAX_PAYLOAD - avail; |
| 332 | |
| 333 | r = s->peer->enqueue(s->peer, p); |
| 334 | |
| 335 | if(r < 0) { |
| 336 | /* error return means they closed us as a side-effect |
| 337 | ** and we must return immediately. |
| 338 | ** |
| 339 | ** note that if we still have buffered packets, the |
| 340 | ** socket will be placed on the closing socket list. |
| 341 | ** this handler function will be called again |
| 342 | ** to process FDE_WRITE events. |
| 343 | */ |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | if(r > 0) { |
| 348 | /* if the remote cannot accept further events, |
| 349 | ** we disable notification of READs. They'll |
| 350 | ** be enabled again when we get a call to ready() |
| 351 | */ |
| 352 | fdevent_del(&s->fde, FDE_READ); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if(is_eof) { |
| 357 | s->close(s); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if(ev & FDE_ERROR){ |
| 362 | /* this should be caught be the next read or write |
| 363 | ** catching it here means we may skip the last few |
| 364 | ** bytes of readable data. |
| 365 | */ |
| 366 | // s->close(s); |
| 367 | return; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | asocket *create_local_socket(int fd) |
| 372 | { |
| 373 | asocket *s = calloc(1, sizeof(asocket)); |
André Goddard Rosa | c419e2a | 2010-06-10 20:48:19 -0300 | [diff] [blame] | 374 | if (s == NULL) fatal("cannot allocate socket"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 375 | install_local_socket(s); |
| 376 | s->fd = fd; |
| 377 | s->enqueue = local_socket_enqueue; |
| 378 | s->ready = local_socket_ready; |
| 379 | s->close = local_socket_close; |
| 380 | |
| 381 | fdevent_install(&s->fde, fd, local_socket_event_func, s); |
| 382 | /* fdevent_add(&s->fde, FDE_ERROR); */ |
| 383 | //fprintf(stderr, "Created local socket in create_local_socket \n"); |
| 384 | D("LS(%d): created (fd=%d)\n", s->id, s->fd); |
| 385 | return s; |
| 386 | } |
| 387 | |
| 388 | asocket *create_local_service_socket(const char *name) |
| 389 | { |
| 390 | asocket *s; |
| 391 | int fd; |
| 392 | |
| 393 | #if !ADB_HOST |
| 394 | if (!strcmp(name,"jdwp")) { |
| 395 | return create_jdwp_service_socket(); |
| 396 | } |
| 397 | if (!strcmp(name,"track-jdwp")) { |
| 398 | return create_jdwp_tracker_service_socket(); |
| 399 | } |
| 400 | #endif |
| 401 | fd = service_to_fd(name); |
| 402 | if(fd < 0) return 0; |
| 403 | |
| 404 | s = create_local_socket(fd); |
| 405 | D("LS(%d): bound to '%s'\n", s->id, name); |
| 406 | return s; |
| 407 | } |
| 408 | |
| 409 | #if ADB_HOST |
| 410 | static asocket *create_host_service_socket(const char *name, const char* serial) |
| 411 | { |
| 412 | asocket *s; |
| 413 | |
| 414 | s = host_service_to_socket(name, serial); |
| 415 | |
| 416 | if (s != NULL) { |
| 417 | D("LS(%d) bound to '%s'\n", s->id, name); |
| 418 | return s; |
| 419 | } |
| 420 | |
| 421 | return s; |
| 422 | } |
| 423 | #endif /* ADB_HOST */ |
| 424 | |
| 425 | /* a Remote socket is used to send/receive data to/from a given transport object |
| 426 | ** it needs to be closed when the transport is forcibly destroyed by the user |
| 427 | */ |
| 428 | typedef struct aremotesocket { |
| 429 | asocket socket; |
| 430 | adisconnect disconnect; |
| 431 | } aremotesocket; |
| 432 | |
| 433 | static int remote_socket_enqueue(asocket *s, apacket *p) |
| 434 | { |
| 435 | D("Calling remote_socket_enqueue\n"); |
| 436 | p->msg.command = A_WRTE; |
| 437 | p->msg.arg0 = s->peer->id; |
| 438 | p->msg.arg1 = s->id; |
| 439 | p->msg.data_length = p->len; |
| 440 | send_packet(p, s->transport); |
| 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | static void remote_socket_ready(asocket *s) |
| 445 | { |
| 446 | D("Calling remote_socket_ready\n"); |
| 447 | apacket *p = get_apacket(); |
| 448 | p->msg.command = A_OKAY; |
| 449 | p->msg.arg0 = s->peer->id; |
| 450 | p->msg.arg1 = s->id; |
| 451 | send_packet(p, s->transport); |
| 452 | } |
| 453 | |
| 454 | static void remote_socket_close(asocket *s) |
| 455 | { |
| 456 | D("Calling remote_socket_close\n"); |
| 457 | apacket *p = get_apacket(); |
| 458 | p->msg.command = A_CLSE; |
| 459 | if(s->peer) { |
| 460 | p->msg.arg0 = s->peer->id; |
| 461 | s->peer->peer = 0; |
| 462 | s->peer->close(s->peer); |
| 463 | } |
| 464 | p->msg.arg1 = s->id; |
| 465 | send_packet(p, s->transport); |
| 466 | D("RS(%d): closed\n", s->id); |
| 467 | remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect ); |
| 468 | free(s); |
| 469 | } |
| 470 | |
| 471 | static void remote_socket_disconnect(void* _s, atransport* t) |
| 472 | { |
| 473 | asocket* s = _s; |
| 474 | asocket* peer = s->peer; |
| 475 | |
| 476 | D("remote_socket_disconnect RS(%d)\n", s->id); |
| 477 | if (peer) { |
| 478 | peer->peer = NULL; |
| 479 | peer->close(peer); |
| 480 | } |
| 481 | remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect ); |
| 482 | free(s); |
| 483 | } |
| 484 | |
| 485 | asocket *create_remote_socket(unsigned id, atransport *t) |
| 486 | { |
| 487 | asocket *s = calloc(1, sizeof(aremotesocket)); |
| 488 | adisconnect* dis = &((aremotesocket*)s)->disconnect; |
| 489 | |
André Goddard Rosa | c419e2a | 2010-06-10 20:48:19 -0300 | [diff] [blame] | 490 | if (s == NULL) fatal("cannot allocate socket"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 491 | s->id = id; |
| 492 | s->enqueue = remote_socket_enqueue; |
| 493 | s->ready = remote_socket_ready; |
| 494 | s->close = remote_socket_close; |
| 495 | s->transport = t; |
| 496 | |
| 497 | dis->func = remote_socket_disconnect; |
| 498 | dis->opaque = s; |
| 499 | add_transport_disconnect( t, dis ); |
| 500 | D("RS(%d): created\n", s->id); |
| 501 | return s; |
| 502 | } |
| 503 | |
| 504 | void connect_to_remote(asocket *s, const char *destination) |
| 505 | { |
| 506 | D("Connect_to_remote call \n"); |
| 507 | apacket *p = get_apacket(); |
| 508 | int len = strlen(destination) + 1; |
| 509 | |
| 510 | if(len > (MAX_PAYLOAD-1)) { |
| 511 | fatal("destination oversized"); |
| 512 | } |
| 513 | |
| 514 | D("LS(%d): connect('%s')\n", s->id, destination); |
| 515 | p->msg.command = A_OPEN; |
| 516 | p->msg.arg0 = s->id; |
| 517 | p->msg.data_length = len; |
| 518 | strcpy((char*) p->data, destination); |
| 519 | send_packet(p, s->transport); |
| 520 | } |
| 521 | |
| 522 | |
| 523 | /* this is used by magic sockets to rig local sockets to |
| 524 | send the go-ahead message when they connect */ |
| 525 | static void local_socket_ready_notify(asocket *s) |
| 526 | { |
| 527 | s->ready = local_socket_ready; |
| 528 | s->close = local_socket_close; |
| 529 | adb_write(s->fd, "OKAY", 4); |
| 530 | s->ready(s); |
| 531 | } |
| 532 | |
| 533 | /* this is used by magic sockets to rig local sockets to |
| 534 | send the failure message if they are closed before |
| 535 | connected (to avoid closing them without a status message) */ |
| 536 | static void local_socket_close_notify(asocket *s) |
| 537 | { |
| 538 | s->ready = local_socket_ready; |
| 539 | s->close = local_socket_close; |
| 540 | sendfailmsg(s->fd, "closed"); |
| 541 | s->close(s); |
| 542 | } |
| 543 | |
| 544 | unsigned unhex(unsigned char *s, int len) |
| 545 | { |
| 546 | unsigned n = 0, c; |
| 547 | |
| 548 | while(len-- > 0) { |
| 549 | switch((c = *s++)) { |
| 550 | case '0': case '1': case '2': |
| 551 | case '3': case '4': case '5': |
| 552 | case '6': case '7': case '8': |
| 553 | case '9': |
| 554 | c -= '0'; |
| 555 | break; |
| 556 | case 'a': case 'b': case 'c': |
| 557 | case 'd': case 'e': case 'f': |
| 558 | c = c - 'a' + 10; |
| 559 | break; |
| 560 | case 'A': case 'B': case 'C': |
| 561 | case 'D': case 'E': case 'F': |
| 562 | c = c - 'A' + 10; |
| 563 | break; |
| 564 | default: |
| 565 | return 0xffffffff; |
| 566 | } |
| 567 | |
| 568 | n = (n << 4) | c; |
| 569 | } |
| 570 | |
| 571 | return n; |
| 572 | } |
| 573 | |
Terence Haddock | e994f18 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 574 | /* skip_host_serial return the position in a string |
| 575 | skipping over the 'serial' parameter in the ADB protocol, |
| 576 | where parameter string may be a host:port string containing |
| 577 | the protocol delimiter (colon). */ |
| 578 | char *skip_host_serial(char *service) { |
| 579 | char *first_colon, *serial_end; |
| 580 | |
| 581 | first_colon = strchr(service, ':'); |
| 582 | if (!first_colon) { |
| 583 | /* No colon in service string. */ |
| 584 | return NULL; |
| 585 | } |
| 586 | serial_end = first_colon; |
| 587 | if (isdigit(serial_end[1])) { |
| 588 | serial_end++; |
| 589 | while ((*serial_end) && isdigit(*serial_end)) { |
| 590 | serial_end++; |
| 591 | } |
| 592 | if ((*serial_end) != ':') { |
| 593 | // Something other than numbers was found, reset the end. |
| 594 | serial_end = first_colon; |
| 595 | } |
| 596 | } |
| 597 | return serial_end; |
| 598 | } |
| 599 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 600 | static int smart_socket_enqueue(asocket *s, apacket *p) |
| 601 | { |
| 602 | unsigned len; |
| 603 | #if ADB_HOST |
| 604 | char *service = NULL; |
| 605 | char* serial = NULL; |
| 606 | transport_type ttype = kTransportAny; |
| 607 | #endif |
| 608 | |
| 609 | D("SS(%d): enqueue %d\n", s->id, p->len); |
| 610 | |
| 611 | if(s->pkt_first == 0) { |
| 612 | s->pkt_first = p; |
| 613 | s->pkt_last = p; |
| 614 | } else { |
| 615 | if((s->pkt_first->len + p->len) > MAX_PAYLOAD) { |
| 616 | D("SS(%d): overflow\n", s->id); |
| 617 | put_apacket(p); |
| 618 | goto fail; |
| 619 | } |
| 620 | |
| 621 | memcpy(s->pkt_first->data + s->pkt_first->len, |
| 622 | p->data, p->len); |
| 623 | s->pkt_first->len += p->len; |
| 624 | put_apacket(p); |
| 625 | |
| 626 | p = s->pkt_first; |
| 627 | } |
| 628 | |
| 629 | /* don't bother if we can't decode the length */ |
| 630 | if(p->len < 4) return 0; |
| 631 | |
| 632 | len = unhex(p->data, 4); |
| 633 | if((len < 1) || (len > 1024)) { |
| 634 | D("SS(%d): bad size (%d)\n", s->id, len); |
| 635 | goto fail; |
| 636 | } |
| 637 | |
| 638 | D("SS(%d): len is %d\n", s->id, len ); |
| 639 | /* can't do anything until we have the full header */ |
| 640 | if((len + 4) > p->len) { |
| 641 | D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len); |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | p->data[len + 4] = 0; |
| 646 | |
| 647 | D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4)); |
| 648 | |
| 649 | #if ADB_HOST |
| 650 | service = (char *)p->data + 4; |
| 651 | if(!strncmp(service, "host-serial:", strlen("host-serial:"))) { |
| 652 | char* serial_end; |
| 653 | service += strlen("host-serial:"); |
| 654 | |
Terence Haddock | e994f18 | 2011-03-16 09:43:56 +0100 | [diff] [blame] | 655 | // serial number should follow "host:" and could be a host:port string. |
| 656 | serial_end = skip_host_serial(service); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 657 | if (serial_end) { |
| 658 | *serial_end = 0; // terminate string |
| 659 | serial = service; |
| 660 | service = serial_end + 1; |
| 661 | } |
| 662 | } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) { |
| 663 | ttype = kTransportUsb; |
| 664 | service += strlen("host-usb:"); |
| 665 | } else if (!strncmp(service, "host-local:", strlen("host-local:"))) { |
| 666 | ttype = kTransportLocal; |
| 667 | service += strlen("host-local:"); |
| 668 | } else if (!strncmp(service, "host:", strlen("host:"))) { |
| 669 | ttype = kTransportAny; |
| 670 | service += strlen("host:"); |
| 671 | } else { |
| 672 | service = NULL; |
| 673 | } |
| 674 | |
| 675 | if (service) { |
| 676 | asocket *s2; |
| 677 | |
| 678 | /* some requests are handled immediately -- in that |
| 679 | ** case the handle_host_request() routine has sent |
| 680 | ** the OKAY or FAIL message and all we have to do |
| 681 | ** is clean up. |
| 682 | */ |
| 683 | if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) { |
| 684 | /* XXX fail message? */ |
| 685 | D( "SS(%d): handled host service '%s'\n", s->id, service ); |
| 686 | goto fail; |
| 687 | } |
| 688 | if (!strncmp(service, "transport", strlen("transport"))) { |
| 689 | D( "SS(%d): okay transport\n", s->id ); |
| 690 | p->len = 0; |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | /* try to find a local service with this name. |
| 695 | ** if no such service exists, we'll fail out |
| 696 | ** and tear down here. |
| 697 | */ |
| 698 | s2 = create_host_service_socket(service, serial); |
| 699 | if(s2 == 0) { |
| 700 | D( "SS(%d): couldn't create host service '%s'\n", s->id, service ); |
| 701 | sendfailmsg(s->peer->fd, "unknown host service"); |
| 702 | goto fail; |
| 703 | } |
| 704 | |
| 705 | /* we've connected to a local host service, |
| 706 | ** so we make our peer back into a regular |
| 707 | ** local socket and bind it to the new local |
| 708 | ** service socket, acknowledge the successful |
| 709 | ** connection, and close this smart socket now |
| 710 | ** that its work is done. |
| 711 | */ |
| 712 | adb_write(s->peer->fd, "OKAY", 4); |
| 713 | |
| 714 | s->peer->ready = local_socket_ready; |
| 715 | s->peer->close = local_socket_close; |
| 716 | s->peer->peer = s2; |
| 717 | s2->peer = s->peer; |
| 718 | s->peer = 0; |
| 719 | D( "SS(%d): okay\n", s->id ); |
| 720 | s->close(s); |
| 721 | |
| 722 | /* initial state is "ready" */ |
| 723 | s2->ready(s2); |
| 724 | return 0; |
| 725 | } |
| 726 | #else /* !ADB_HOST */ |
| 727 | if (s->transport == NULL) { |
| 728 | char* error_string = "unknown failure"; |
| 729 | s->transport = acquire_one_transport (CS_ANY, |
| 730 | kTransportAny, NULL, &error_string); |
| 731 | |
| 732 | if (s->transport == NULL) { |
| 733 | sendfailmsg(s->peer->fd, error_string); |
| 734 | goto fail; |
| 735 | } |
| 736 | } |
| 737 | #endif |
| 738 | |
| 739 | if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) { |
| 740 | /* if there's no remote we fail the connection |
| 741 | ** right here and terminate it |
| 742 | */ |
| 743 | sendfailmsg(s->peer->fd, "device offline (x)"); |
| 744 | goto fail; |
| 745 | } |
| 746 | |
| 747 | |
| 748 | /* instrument our peer to pass the success or fail |
| 749 | ** message back once it connects or closes, then |
| 750 | ** detach from it, request the connection, and |
| 751 | ** tear down |
| 752 | */ |
| 753 | s->peer->ready = local_socket_ready_notify; |
| 754 | s->peer->close = local_socket_close_notify; |
| 755 | s->peer->peer = 0; |
| 756 | /* give him our transport and upref it */ |
| 757 | s->peer->transport = s->transport; |
| 758 | |
| 759 | connect_to_remote(s->peer, (char*) (p->data + 4)); |
| 760 | s->peer = 0; |
| 761 | s->close(s); |
| 762 | return 1; |
| 763 | |
| 764 | fail: |
| 765 | /* we're going to close our peer as a side-effect, so |
| 766 | ** return -1 to signal that state to the local socket |
| 767 | ** who is enqueueing against us |
| 768 | */ |
| 769 | s->close(s); |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | static void smart_socket_ready(asocket *s) |
| 774 | { |
| 775 | D("SS(%d): ready\n", s->id); |
| 776 | } |
| 777 | |
| 778 | static void smart_socket_close(asocket *s) |
| 779 | { |
| 780 | D("SS(%d): closed\n", s->id); |
| 781 | if(s->pkt_first){ |
| 782 | put_apacket(s->pkt_first); |
| 783 | } |
| 784 | if(s->peer) { |
| 785 | s->peer->peer = 0; |
| 786 | s->peer->close(s->peer); |
Tom Marlin | 49f1857 | 2011-05-13 13:24:55 -0500 | [diff] [blame^] | 787 | s->peer = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 788 | } |
| 789 | free(s); |
| 790 | } |
| 791 | |
| 792 | asocket *create_smart_socket(void (*action_cb)(asocket *s, const char *act)) |
| 793 | { |
| 794 | D("Creating smart socket \n"); |
| 795 | asocket *s = calloc(1, sizeof(asocket)); |
André Goddard Rosa | c419e2a | 2010-06-10 20:48:19 -0300 | [diff] [blame] | 796 | if (s == NULL) fatal("cannot allocate socket"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 797 | s->enqueue = smart_socket_enqueue; |
| 798 | s->ready = smart_socket_ready; |
| 799 | s->close = smart_socket_close; |
| 800 | s->extra = action_cb; |
| 801 | |
| 802 | D("SS(%d): created %p\n", s->id, action_cb); |
| 803 | return s; |
| 804 | } |
| 805 | |
| 806 | void smart_socket_action(asocket *s, const char *act) |
| 807 | { |
| 808 | |
| 809 | } |
| 810 | |
| 811 | void connect_to_smartsocket(asocket *s) |
| 812 | { |
| 813 | D("Connecting to smart socket \n"); |
| 814 | asocket *ss = create_smart_socket(smart_socket_action); |
| 815 | s->peer = ss; |
| 816 | ss->peer = s; |
| 817 | s->ready(s); |
| 818 | } |