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