blob: c012df84f7a3c4e776fb67a7cbf0b9a211cc63ce [file] [log] [blame]
Yifan Hongf6b4d5c2021-06-23 19:12:32 -07001/*
2 * Copyright (C) 2021 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#define LOG_TAG "RpcRawTransport"
18#include <log/log.h>
19
Yifan Hong8c950422021-08-05 17:13:55 -070020#include <poll.h>
21
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070022#include <binder/RpcTransportRaw.h>
23
Yifan Hong8c950422021-08-05 17:13:55 -070024#include "FdTrigger.h"
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070025#include "RpcState.h"
26
27using android::base::ErrnoError;
28using android::base::Result;
29
30namespace android {
31
32namespace {
33
34// RpcTransport with TLS disabled.
35class RpcTransportRaw : public RpcTransport {
36public:
37 explicit RpcTransportRaw(android::base::unique_fd socket) : mSocket(std::move(socket)) {}
Yifan Hong8c950422021-08-05 17:13:55 -070038 Result<size_t> send(const void* buf, size_t size) {
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070039 ssize_t ret = TEMP_FAILURE_RETRY(::send(mSocket.get(), buf, size, MSG_NOSIGNAL));
40 if (ret < 0) {
41 return ErrnoError() << "send()";
42 }
43 return ret;
44 }
Yifan Hong8c950422021-08-05 17:13:55 -070045 Result<size_t> recv(void* buf, size_t size) {
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070046 ssize_t ret = TEMP_FAILURE_RETRY(::recv(mSocket.get(), buf, size, MSG_NOSIGNAL));
47 if (ret < 0) {
48 return ErrnoError() << "recv()";
49 }
50 return ret;
51 }
Yifan Hong218c4072021-08-04 14:59:10 -070052 Result<size_t> peek(void *buf, size_t size) override {
Yifan Hongb675ffe2021-08-05 16:37:17 -070053 ssize_t ret = TEMP_FAILURE_RETRY(::recv(mSocket.get(), buf, size, MSG_PEEK));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070054 if (ret < 0) {
55 return ErrnoError() << "recv(MSG_PEEK)";
56 }
57 return ret;
58 }
Yifan Hong8c950422021-08-05 17:13:55 -070059
60 status_t interruptableWriteFully(FdTrigger* fdTrigger, const void* data, size_t size) override {
61 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data);
62 const uint8_t* end = buffer + size;
63
64 MAYBE_WAIT_IN_FLAKE_MODE;
65
66 status_t status;
67 while ((status = fdTrigger->triggerablePoll(mSocket.get(), POLLOUT)) == OK) {
68 auto writeSize = this->send(buffer, end - buffer);
69 if (!writeSize.ok()) {
70 LOG_RPC_DETAIL("RpcTransport::send(): %s", writeSize.error().message().c_str());
71 return writeSize.error().code() == 0 ? UNKNOWN_ERROR : -writeSize.error().code();
72 }
73
74 if (*writeSize == 0) return DEAD_OBJECT;
75
76 buffer += *writeSize;
77 if (buffer == end) return OK;
78 }
79 return status;
80 }
81
82 status_t interruptableReadFully(FdTrigger* fdTrigger, void* data, size_t size) override {
83 uint8_t* buffer = reinterpret_cast<uint8_t*>(data);
84 uint8_t* end = buffer + size;
85
86 MAYBE_WAIT_IN_FLAKE_MODE;
87
88 status_t status;
89 while ((status = fdTrigger->triggerablePoll(mSocket.get(), POLLIN)) == OK) {
90 auto readSize = this->recv(buffer, end - buffer);
91 if (!readSize.ok()) {
92 LOG_RPC_DETAIL("RpcTransport::recv(): %s", readSize.error().message().c_str());
93 return readSize.error().code() == 0 ? UNKNOWN_ERROR : -readSize.error().code();
94 }
95
96 if (*readSize == 0) return DEAD_OBJECT; // EOF
97
98 buffer += *readSize;
99 if (buffer == end) return OK;
100 }
101 return status;
102 }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700103
104private:
105 android::base::unique_fd mSocket;
106};
107
108// RpcTransportCtx with TLS disabled.
109class RpcTransportCtxRaw : public RpcTransportCtx {
110public:
Yifan Hongf6d42292021-08-05 23:43:05 -0700111 std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd, FdTrigger*) const {
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700112 return std::make_unique<RpcTransportRaw>(std::move(fd));
113 }
Yifan Hong9734cfc2021-09-13 16:14:09 -0700114 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700115};
Yifan Hong588d59c2021-08-16 17:13:58 -0700116
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700117} // namespace
118
119std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
120 return std::make_unique<RpcTransportCtxRaw>();
121}
122
123std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
124 return std::make_unique<RpcTransportCtxRaw>();
125}
126
127const char *RpcTransportCtxFactoryRaw::toCString() const {
128 return "raw";
129}
130
131std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
132 return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
133}
134
135} // namespace android