Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef __ADB_SOCKET_H |
| 18 | #define __ADB_SOCKET_H |
| 19 | |
| 20 | #include <stddef.h> |
| 21 | |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 22 | #include <deque> |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 23 | #include <memory> |
Josh Gao | bd1f300 | 2021-12-29 21:15:12 -0800 | [diff] [blame] | 24 | #include <optional> |
Josh Gao | a7d9d71 | 2018-02-01 13:17:50 -0800 | [diff] [blame] | 25 | #include <string> |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 26 | |
Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 27 | #include "adb_unique_fd.h" |
Josh Gao | b51193a | 2019-06-28 13:50:37 -0700 | [diff] [blame] | 28 | #include "fdevent/fdevent.h" |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 29 | #include "types.h" |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 30 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 31 | class atransport; |
| 32 | |
| 33 | /* An asocket represents one half of a connection between a local and |
Fabien Sanglard | 727a1b3 | 2022-09-07 16:16:07 -0700 | [diff] [blame] | 34 | remote entity. A local asocket is bound to a file descriptor. A |
| 35 | remote asocket is bound to the protocol engine. |
| 36 | |
| 37 | Example (two local_sockets) : |
| 38 | |
| 39 | ASOCKET(THIS) |
| 40 | ┌────────────────────────────────────────────────┐ |
| 41 | ┌──┐ write(3) │ ┌─────┐ enqueue() │ |
| 42 | │ │◄─────────┼──┤Queue├─────────────◄────────────┐ │ |
| 43 | │fd│ │ └─────┘ ▲ │ |
| 44 | │ ├──────────►─────────────────┐ │ │ |
| 45 | └──┘ read(3) └─────────────────┼─────────────────┼────────────┘ |
| 46 | outgoing │ │ incoming |
| 47 | ┌─────────────────▼─────────────────▲────────────┐ read(3) ┌──┐ |
| 48 | │ │ └────────────┼─────────◄─┤ │ |
| 49 | │ │ ┌─────┐ │ │fd│ |
| 50 | │ └─────────────────────►│Queue├─┼─────────►─┤ │ |
| 51 | │ enqueue() └─────┘ │ write(3) └──┘ |
| 52 | └────────────────────────────────────────────────┘ |
| 53 | ASOCKET(PEER) |
| 54 | |
| 55 | Note that sockets can be peered regardless of their kind. A remote socket can be peered with |
| 56 | a smart socket, a local socket can be peered with a remote socket and so on. |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 57 | */ |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 58 | struct asocket { |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 59 | /* the unique identifier for this asocket |
| 60 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 61 | unsigned id = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 62 | |
Fabien Sanglard | 727a1b3 | 2022-09-07 16:16:07 -0700 | [diff] [blame] | 63 | // Start Local socket fields |
| 64 | // TODO: move all the local socket fields together |
| 65 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 66 | /* flag: set when the socket's peer has closed |
| 67 | * but packets are still queued for delivery |
| 68 | */ |
Fabien Sanglard | b69c59c | 2022-09-12 09:38:19 -0700 | [diff] [blame] | 69 | bool closing = false; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 70 | |
| 71 | // flag: set when the socket failed to write, so the socket will not wait to |
| 72 | // write packets and close directly. |
Fabien Sanglard | 36be8b0 | 2022-09-12 09:52:51 -0700 | [diff] [blame] | 73 | bool has_write_error = false; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 74 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 75 | /* flag: quit adbd when both ends close the |
| 76 | * local service socket |
| 77 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 78 | int exit_on_close = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 79 | |
Fabien Sanglard | 727a1b3 | 2022-09-07 16:16:07 -0700 | [diff] [blame] | 80 | // End Local socket fields |
| 81 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 82 | // the asocket we are connected to |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 83 | asocket* peer = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 84 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 85 | /* enqueue is called by our peer when it has data |
| 86 | * for us. It should return 0 if we can accept more |
| 87 | * data or 1 if not. If we return 1, we must call |
| 88 | * peer->ready() when we once again are ready to |
| 89 | * receive data. |
| 90 | */ |
Josh Gao | cd2a529 | 2018-03-07 16:52:28 -0800 | [diff] [blame] | 91 | int (*enqueue)(asocket* s, apacket::payload_type data) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 92 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 93 | /* ready is called by the peer when it is ready for |
| 94 | * us to send data via enqueue again |
| 95 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 96 | void (*ready)(asocket* s) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 97 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 98 | /* shutdown is called by the peer before it goes away. |
| 99 | * the socket should not do any further calls on its peer. |
| 100 | * Always followed by a call to close. Optional, i.e. can be NULL. |
| 101 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 102 | void (*shutdown)(asocket* s) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 103 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 104 | /* close is called by the peer when it has gone away. |
| 105 | * we are not allowed to make any further calls on the |
| 106 | * peer once our close method is called. |
| 107 | */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 108 | void (*close)(asocket* s) = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 109 | |
Josh Gao | 2f8da95 | 2017-09-12 13:23:33 -0700 | [diff] [blame] | 110 | /* A socket is bound to atransport */ |
Josh Gao | 5cb76ce | 2018-02-12 17:24:00 -0800 | [diff] [blame] | 111 | atransport* transport = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 112 | |
| 113 | size_t get_max_payload() const; |
Josh Gao | bd1f300 | 2021-12-29 21:15:12 -0800 | [diff] [blame] | 114 | |
Josh Gao | bd1f300 | 2021-12-29 21:15:12 -0800 | [diff] [blame] | 115 | // TODO: Make asocket an actual class and use inheritance instead of having an ever-growing |
| 116 | // struct with random use-specific fields stuffed into it. |
Fabien Sanglard | 727a1b3 | 2022-09-07 16:16:07 -0700 | [diff] [blame] | 117 | |
| 118 | // Start Local socket fields |
Josh Gao | bd1f300 | 2021-12-29 21:15:12 -0800 | [diff] [blame] | 119 | fdevent* fde = nullptr; |
| 120 | int fd = -1; |
| 121 | |
| 122 | // Queue of data that we've received from our peer, and are waiting to write into fd. |
| 123 | IOVector packet_queue; |
Fabien Sanglard | 727a1b3 | 2022-09-07 16:16:07 -0700 | [diff] [blame] | 124 | // End Local socket fields |
Josh Gao | bd1f300 | 2021-12-29 21:15:12 -0800 | [diff] [blame] | 125 | |
| 126 | // The number of bytes that have been acknowledged by the other end if delayed_ack is available. |
| 127 | // This value can go negative: if we have a MAX_PAYLOAD's worth of bytes available to send, |
| 128 | // we'll send out a full packet. |
| 129 | std::optional<int64_t> available_send_bytes; |
| 130 | |
Fabien Sanglard | 727a1b3 | 2022-09-07 16:16:07 -0700 | [diff] [blame] | 131 | // Start Smart socket fields |
Josh Gao | bd1f300 | 2021-12-29 21:15:12 -0800 | [diff] [blame] | 132 | // A temporary buffer used to hold a partially-read service string for smartsockets. |
| 133 | std::string smart_socket_data; |
Fabien Sanglard | 727a1b3 | 2022-09-07 16:16:07 -0700 | [diff] [blame] | 134 | // End Smart socket fields |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | asocket *find_local_socket(unsigned local_id, unsigned remote_id); |
| 138 | void install_local_socket(asocket *s); |
| 139 | void remove_socket(asocket *s); |
| 140 | void close_all_sockets(atransport *t); |
| 141 | |
Josh Gao | bd1f300 | 2021-12-29 21:15:12 -0800 | [diff] [blame] | 142 | void local_socket_ack(asocket* s, std::optional<int32_t> acked_bytes); |
| 143 | |
Josh Gao | c270596 | 2019-01-23 15:36:42 -0800 | [diff] [blame] | 144 | asocket* create_local_socket(unique_fd fd); |
Josh Gao | 6dbf579 | 2018-12-13 14:21:00 -0800 | [diff] [blame] | 145 | asocket* create_local_service_socket(std::string_view destination, atransport* transport); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 146 | |
| 147 | asocket *create_remote_socket(unsigned id, atransport *t); |
Josh Gao | 4a037e2 | 2018-12-20 17:00:13 -0800 | [diff] [blame] | 148 | void connect_to_remote(asocket* s, std::string_view destination); |
Josh Gao | 65d18e2 | 2020-04-22 20:57:26 -0700 | [diff] [blame] | 149 | |
| 150 | #if ADB_HOST |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 151 | void connect_to_smartsocket(asocket *s); |
Josh Gao | 65d18e2 | 2020-04-22 20:57:26 -0700 | [diff] [blame] | 152 | #endif |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 153 | |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 154 | // Internal functions that are only made available here for testing purposes. |
| 155 | namespace internal { |
| 156 | |
| 157 | #if ADB_HOST |
Josh Gao | 8d49e12 | 2018-12-19 13:37:41 -0800 | [diff] [blame] | 158 | bool parse_host_service(std::string_view* out_serial, std::string_view* out_command, |
| 159 | std::string_view service); |
David Pursell | c929c6f | 2016-03-01 08:58:26 -0800 | [diff] [blame] | 160 | #endif |
| 161 | |
| 162 | } // namespace internal |
| 163 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 164 | #endif // __ADB_SOCKET_H |