Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 20 | #include <binder/RpcTransportRaw.h> |
| 21 | |
| 22 | #include "RpcState.h" |
| 23 | |
| 24 | using android::base::ErrnoError; |
| 25 | using android::base::Result; |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | // RpcTransport with TLS disabled. |
| 32 | class RpcTransportRaw : public RpcTransport { |
| 33 | public: |
| 34 | explicit RpcTransportRaw(android::base::unique_fd socket) : mSocket(std::move(socket)) {} |
| 35 | Result<ssize_t> send(const void *buf, int size) override { |
| 36 | ssize_t ret = TEMP_FAILURE_RETRY(::send(mSocket.get(), buf, size, MSG_NOSIGNAL)); |
| 37 | if (ret < 0) { |
| 38 | return ErrnoError() << "send()"; |
| 39 | } |
| 40 | return ret; |
| 41 | } |
| 42 | Result<ssize_t> recv(void *buf, int size) override { |
| 43 | ssize_t ret = TEMP_FAILURE_RETRY(::recv(mSocket.get(), buf, size, MSG_NOSIGNAL)); |
| 44 | if (ret < 0) { |
| 45 | return ErrnoError() << "recv()"; |
| 46 | } |
| 47 | return ret; |
| 48 | } |
| 49 | Result<ssize_t> peek(void *buf, int size) override { |
| 50 | ssize_t ret = TEMP_FAILURE_RETRY(::recv(mSocket.get(), buf, size, MSG_PEEK | MSG_DONTWAIT)); |
| 51 | if (ret < 0) { |
| 52 | return ErrnoError() << "recv(MSG_PEEK)"; |
| 53 | } |
| 54 | return ret; |
| 55 | } |
| 56 | bool pending() override { return false; } |
| 57 | android::base::borrowed_fd pollSocket() const override { return mSocket; } |
| 58 | |
| 59 | private: |
| 60 | android::base::unique_fd mSocket; |
| 61 | }; |
| 62 | |
| 63 | // RpcTransportCtx with TLS disabled. |
| 64 | class RpcTransportCtxRaw : public RpcTransportCtx { |
| 65 | public: |
| 66 | std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd) const { |
| 67 | return std::make_unique<RpcTransportRaw>(std::move(fd)); |
| 68 | } |
| 69 | }; |
| 70 | } // namespace |
| 71 | |
| 72 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const { |
| 73 | return std::make_unique<RpcTransportCtxRaw>(); |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const { |
| 77 | return std::make_unique<RpcTransportCtxRaw>(); |
| 78 | } |
| 79 | |
| 80 | const char *RpcTransportCtxFactoryRaw::toCString() const { |
| 81 | return "raw"; |
| 82 | } |
| 83 | |
| 84 | std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() { |
| 85 | return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw()); |
| 86 | } |
| 87 | |
| 88 | } // namespace android |