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 22 #include <deque> 23 #include <memory> 24 #include <optional> 25 #include <string> 26 27 #include "adb_unique_fd.h" 28 #include "fdevent/fdevent.h" 29 #include "types.h" 30 31 class atransport; 32 33 /* An asocket represents one half of a connection between a local and 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. 57 */ 58 struct asocket { 59 /* the unique identifier for this asocket 60 */ 61 unsigned id = 0; 62 63 // Start Local socket fields 64 // TODO: move all the local socket fields together 65 66 /* flag: set when the socket's peer has closed 67 * but packets are still queued for delivery 68 * TODO: This should be a boolean. 69 */ 70 bool closing = false; 71 72 // flag: set when the socket failed to write, so the socket will not wait to 73 // write packets and close directly. 74 bool has_write_error = false; 75 76 /* flag: quit adbd when both ends close the 77 * local service socket 78 */ 79 int exit_on_close = 0; 80 81 // End Local socket fields 82 83 // the asocket we are connected to 84 asocket* peer = nullptr; 85 86 /* enqueue is called by our peer when it has data 87 * for us. It should return 0 if we can accept more 88 * data or 1 if not. If we return 1, we must call 89 * peer->ready() when we once again are ready to 90 * receive data. 91 */ 92 int (*enqueue)(asocket* s, apacket::payload_type data) = nullptr; 93 94 /* ready is called by the peer when it is ready for 95 * us to send data via enqueue again 96 */ 97 void (*ready)(asocket* s) = nullptr; 98 99 /* shutdown is called by the peer before it goes away. 100 * the socket should not do any further calls on its peer. 101 * Always followed by a call to close. Optional, i.e. can be NULL. 102 */ 103 void (*shutdown)(asocket* s) = nullptr; 104 105 /* close is called by the peer when it has gone away. 106 * we are not allowed to make any further calls on the 107 * peer once our close method is called. 108 */ 109 void (*close)(asocket* s) = nullptr; 110 111 /* A socket is bound to atransport */ 112 atransport* transport = nullptr; 113 114 size_t get_max_payload() const; 115 116 // TODO: Make asocket an actual class and use inheritance instead of having an ever-growing 117 // struct with random use-specific fields stuffed into it. 118 119 // Start Local socket fields 120 fdevent* fde = nullptr; 121 int fd = -1; 122 123 // Queue of data that we've received from our peer, and are waiting to write into fd. 124 IOVector packet_queue; 125 // End Local socket fields 126 127 // The number of bytes that have been acknowledged by the other end if delayed_ack is available. 128 // This value can go negative: if we have a MAX_PAYLOAD's worth of bytes available to send, 129 // we'll send out a full packet. 130 std::optional<int64_t> available_send_bytes; 131 132 // Start Smart socket fields 133 // A temporary buffer used to hold a partially-read service string for smartsockets. 134 std::string smart_socket_data; 135 // End Smart socket fields 136 }; 137 138 asocket *find_local_socket(unsigned local_id, unsigned remote_id); 139 void install_local_socket(asocket *s); 140 void remove_socket(asocket *s); 141 void close_all_sockets(atransport *t); 142 143 void local_socket_ack(asocket* s, std::optional<int32_t> acked_bytes); 144 145 asocket* create_local_socket(unique_fd fd); 146 asocket* create_local_service_socket(std::string_view destination, atransport* transport); 147 148 asocket *create_remote_socket(unsigned id, atransport *t); 149 void connect_to_remote(asocket* s, std::string_view destination); 150 151 #if ADB_HOST 152 void connect_to_smartsocket(asocket *s); 153 #endif 154 155 // Internal functions that are only made available here for testing purposes. 156 namespace internal { 157 158 #if ADB_HOST 159 bool parse_host_service(std::string_view* out_serial, std::string_view* out_command, 160 std::string_view service); 161 #endif 162 163 } // namespace internal 164 165 #endif // __ADB_SOCKET_H 166