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