1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef ART_DT_FD_FORWARD_EXPORT_FD_TRANSPORT_H_
17 #define ART_DT_FD_FORWARD_EXPORT_FD_TRANSPORT_H_
18 
19 #include <stdint.h>
20 
21 namespace dt_fd_forward {
22 
23 // The file-descriptors sent over a socket to the dt_fd_forward transport.
24 struct FdSet {
25   // A fd that can be read from which provides the JDWP data.
26   int read_fd_;
27 
28   // A fd that can be written to in order to provide JDWP responses and events.
29   int write_fd_;
30 
31   // A eventfd that can be locked to ensure that writes to write_fd_ are atomic. This must be held
32   // when writing to write_fd_. This allows the proxy to insert packets into the response stream
33   // without having to parse it.
34   int write_lock_fd_;
35 
36   static constexpr size_t kDataLength = sizeof(int) * 3;
WriteDataFdSet37   void WriteData(void* buf) {
38     int* ibuf = reinterpret_cast<int*>(buf);
39     ibuf[0] = read_fd_;
40     ibuf[1] = write_fd_;
41     ibuf[2] = write_lock_fd_;
42   }
43 
ReadDataFdSet44   static FdSet ReadData(void* buf) {
45     int* ibuf = reinterpret_cast<int*>(buf);
46     return FdSet { ibuf[0], ibuf[1], ibuf[2] };
47   }
48 };
49 
50 // Sent with the file descriptors if the transport should not skip waiting for the handshake.
51 static constexpr char kPerformHandshakeMessage[] = "HANDSHAKE:REQD";
52 
53 // Sent with the file descriptors if the transport can skip waiting for the handshake.
54 static constexpr char kSkipHandshakeMessage[] = "HANDSHAKE:SKIP";
55 
56 // This message is sent over the fd associated with the transport when we are listening for fds.
57 static constexpr char kListenStartMessage[] = "dt_fd_forward:START-LISTEN";
58 
59 // This message is sent over the fd associated with the transport when we stop listening for fds.
60 static constexpr char kListenEndMessage[] = "dt_fd_forward:END-LISTEN";
61 
62 // This message is sent over the fd associated with the transport when we have accepted a
63 // connection. This is sent before any handshaking has occurred. It is simply an acknowledgment
64 // that the FdSet has been received. This will be paired with a single CLOSING message when these
65 // fds are closed.
66 static constexpr char kAcceptMessage[] = "dt_fd_forward:ACCEPTED";
67 
68 // This message is sent over the fd associated with the transport when we have
69 // completed the handshake. If the handshake was already performed this is sent
70 // immediately.
71 static constexpr char kHandshakeCompleteMessage[] = "dt_fd_forward:HANDSHAKE-COMPLETE";
72 
73 // This message is sent over the fd associated with the transport when we are closing the fds. This
74 // can be used by the proxy to send additional data on a dup'd fd. The write_lock_fd_ will be held
75 // until the other two fds are closed and then it will be released and closed.
76 static constexpr char kCloseMessage[] = "dt_fd_forward:CLOSING";
77 
78 }  // namespace dt_fd_forward
79 
80 #endif  // ART_DT_FD_FORWARD_EXPORT_FD_TRANSPORT_H_
81