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