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