blob: a6f7d4ff591e80e525d19cda41fdb88b47e35e6b [file] [log] [blame]
Yabin Cui2ce9d562015-09-15 16:27:09 -07001/*
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 Gaoa7d9d712018-02-01 13:17:50 -080022#include <deque>
Josh Gao2f8da952017-09-12 13:23:33 -070023#include <memory>
Josh Gaobd1f3002021-12-29 21:15:12 -080024#include <optional>
Josh Gaoa7d9d712018-02-01 13:17:50 -080025#include <string>
Josh Gao2f8da952017-09-12 13:23:33 -070026
Josh Gaoc2705962019-01-23 15:36:42 -080027#include "adb_unique_fd.h"
Josh Gaob51193a2019-06-28 13:50:37 -070028#include "fdevent/fdevent.h"
Josh Gaocd2a5292018-03-07 16:52:28 -080029#include "types.h"
Yabin Cui2ce9d562015-09-15 16:27:09 -070030
Yabin Cui2ce9d562015-09-15 16:27:09 -070031class atransport;
32
33/* An asocket represents one half of a connection between a local and
Fabien Sanglard727a1b32022-09-07 16:16:07 -070034 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 Gao2f8da952017-09-12 13:23:33 -070057 */
Yabin Cui2ce9d562015-09-15 16:27:09 -070058struct asocket {
Josh Gao2f8da952017-09-12 13:23:33 -070059 /* the unique identifier for this asocket
60 */
Josh Gao5cb76ce2018-02-12 17:24:00 -080061 unsigned id = 0;
Yabin Cui2ce9d562015-09-15 16:27:09 -070062
Fabien Sanglard727a1b32022-09-07 16:16:07 -070063 // Start Local socket fields
64 // TODO: move all the local socket fields together
65
Josh Gao2f8da952017-09-12 13:23:33 -070066 /* flag: set when the socket's peer has closed
67 * but packets are still queued for delivery
68 */
Fabien Sanglardb69c59c2022-09-12 09:38:19 -070069 bool closing = false;
Yabin Cui2ce9d562015-09-15 16:27:09 -070070
71 // flag: set when the socket failed to write, so the socket will not wait to
72 // write packets and close directly.
Fabien Sanglard36be8b02022-09-12 09:52:51 -070073 bool has_write_error = false;
Yabin Cui2ce9d562015-09-15 16:27:09 -070074
Josh Gao2f8da952017-09-12 13:23:33 -070075 /* flag: quit adbd when both ends close the
76 * local service socket
77 */
Josh Gao5cb76ce2018-02-12 17:24:00 -080078 int exit_on_close = 0;
Yabin Cui2ce9d562015-09-15 16:27:09 -070079
Fabien Sanglard727a1b32022-09-07 16:16:07 -070080 // End Local socket fields
81
Josh Gao2f8da952017-09-12 13:23:33 -070082 // the asocket we are connected to
Josh Gao5cb76ce2018-02-12 17:24:00 -080083 asocket* peer = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070084
Josh Gao2f8da952017-09-12 13:23:33 -070085 /* 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 Gaocd2a5292018-03-07 16:52:28 -080091 int (*enqueue)(asocket* s, apacket::payload_type data) = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070092
Josh Gao2f8da952017-09-12 13:23:33 -070093 /* ready is called by the peer when it is ready for
94 * us to send data via enqueue again
95 */
Josh Gao5cb76ce2018-02-12 17:24:00 -080096 void (*ready)(asocket* s) = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070097
Josh Gao2f8da952017-09-12 13:23:33 -070098 /* 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 Gao5cb76ce2018-02-12 17:24:00 -0800102 void (*shutdown)(asocket* s) = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -0700103
Josh Gao2f8da952017-09-12 13:23:33 -0700104 /* 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 Gao5cb76ce2018-02-12 17:24:00 -0800108 void (*close)(asocket* s) = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -0700109
Josh Gao2f8da952017-09-12 13:23:33 -0700110 /* A socket is bound to atransport */
Josh Gao5cb76ce2018-02-12 17:24:00 -0800111 atransport* transport = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -0700112
113 size_t get_max_payload() const;
Josh Gaobd1f3002021-12-29 21:15:12 -0800114
Josh Gaobd1f3002021-12-29 21:15:12 -0800115 // 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 Sanglard727a1b32022-09-07 16:16:07 -0700117
118 // Start Local socket fields
Josh Gaobd1f3002021-12-29 21:15:12 -0800119 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 Sanglard727a1b32022-09-07 16:16:07 -0700124 // End Local socket fields
Josh Gaobd1f3002021-12-29 21:15:12 -0800125
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 Sanglard727a1b32022-09-07 16:16:07 -0700131 // Start Smart socket fields
Josh Gaobd1f3002021-12-29 21:15:12 -0800132 // A temporary buffer used to hold a partially-read service string for smartsockets.
133 std::string smart_socket_data;
Fabien Sanglard727a1b32022-09-07 16:16:07 -0700134 // End Smart socket fields
Yabin Cui2ce9d562015-09-15 16:27:09 -0700135};
136
137asocket *find_local_socket(unsigned local_id, unsigned remote_id);
138void install_local_socket(asocket *s);
139void remove_socket(asocket *s);
140void close_all_sockets(atransport *t);
141
Josh Gaobd1f3002021-12-29 21:15:12 -0800142void local_socket_ack(asocket* s, std::optional<int32_t> acked_bytes);
143
Josh Gaoc2705962019-01-23 15:36:42 -0800144asocket* create_local_socket(unique_fd fd);
Josh Gao6dbf5792018-12-13 14:21:00 -0800145asocket* create_local_service_socket(std::string_view destination, atransport* transport);
Yabin Cui2ce9d562015-09-15 16:27:09 -0700146
147asocket *create_remote_socket(unsigned id, atransport *t);
Josh Gao4a037e22018-12-20 17:00:13 -0800148void connect_to_remote(asocket* s, std::string_view destination);
Josh Gao65d18e22020-04-22 20:57:26 -0700149
150#if ADB_HOST
Yabin Cui2ce9d562015-09-15 16:27:09 -0700151void connect_to_smartsocket(asocket *s);
Josh Gao65d18e22020-04-22 20:57:26 -0700152#endif
Yabin Cui2ce9d562015-09-15 16:27:09 -0700153
David Pursellc929c6f2016-03-01 08:58:26 -0800154// Internal functions that are only made available here for testing purposes.
155namespace internal {
156
157#if ADB_HOST
Josh Gao8d49e122018-12-19 13:37:41 -0800158bool parse_host_service(std::string_view* out_serial, std::string_view* out_command,
159 std::string_view service);
David Pursellc929c6f2016-03-01 08:58:26 -0800160#endif
161
162} // namespace internal
163
Yabin Cui2ce9d562015-09-15 16:27:09 -0700164#endif // __ADB_SOCKET_H