Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #include <binder/RpcAddress.h> |
| 18 | |
Steven Moreland | 6212901 | 2021-07-29 12:14:44 -0700 | [diff] [blame] | 19 | #include <android-base/hex.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 20 | #include <binder/Parcel.h> |
| 21 | |
| 22 | #include "Debug.h" |
| 23 | #include "RpcState.h" |
| 24 | #include "RpcWireFormat.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | RpcAddress RpcAddress::zero() { |
| 29 | return RpcAddress(); |
| 30 | } |
| 31 | |
| 32 | bool RpcAddress::isZero() const { |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 33 | RpcWireAddress ZERO{.options = 0}; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 34 | return memcmp(mRawAddr.get(), &ZERO, sizeof(RpcWireAddress)) == 0; |
| 35 | } |
| 36 | |
| 37 | static void ReadRandomBytes(uint8_t* buf, size_t len) { |
| 38 | int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 39 | if (fd == -1) { |
| 40 | ALOGE("%s: cannot read /dev/urandom", __func__); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | size_t n; |
| 45 | while ((n = TEMP_FAILURE_RETRY(read(fd, buf, len))) > 0) { |
| 46 | len -= n; |
| 47 | buf += n; |
| 48 | } |
| 49 | if (len > 0) { |
| 50 | ALOGW("%s: there are %d bytes skipped", __func__, (int)len); |
| 51 | } |
| 52 | close(fd); |
| 53 | } |
| 54 | |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 55 | RpcAddress RpcAddress::random(bool forServer) { |
| 56 | // The remainder of this header acts as reserved space for different kinds |
| 57 | // of binder objects. |
| 58 | uint64_t options = RPC_WIRE_ADDRESS_OPTION_CREATED; |
| 59 | |
| 60 | // servers and clients allocate addresses independently, so this bit can |
| 61 | // tell you where an address originates |
| 62 | if (forServer) options |= RPC_WIRE_ADDRESS_OPTION_FOR_SERVER; |
| 63 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 64 | RpcAddress ret; |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 65 | RpcWireAddress* raw = ret.mRawAddr.get(); |
| 66 | |
| 67 | raw->options = options; |
| 68 | ReadRandomBytes(raw->address, sizeof(raw->address)); |
| 69 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 70 | LOG_RPC_DETAIL("Creating new address: %s", ret.toString().c_str()); |
| 71 | return ret; |
| 72 | } |
| 73 | |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 74 | bool RpcAddress::isForServer() const { |
| 75 | return mRawAddr.get()->options & RPC_WIRE_ADDRESS_OPTION_FOR_SERVER; |
| 76 | } |
| 77 | |
| 78 | bool RpcAddress::isRecognizedType() const { |
| 79 | uint64_t allKnownOptions = RPC_WIRE_ADDRESS_OPTION_CREATED | RPC_WIRE_ADDRESS_OPTION_FOR_SERVER; |
| 80 | return (mRawAddr.get()->options & ~allKnownOptions) == 0; |
| 81 | } |
| 82 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 83 | RpcAddress RpcAddress::fromRawEmbedded(const RpcWireAddress* raw) { |
| 84 | RpcAddress addr; |
| 85 | memcpy(addr.mRawAddr.get(), raw, sizeof(RpcWireAddress)); |
| 86 | return addr; |
| 87 | } |
| 88 | |
| 89 | const RpcWireAddress& RpcAddress::viewRawEmbedded() const { |
| 90 | return *mRawAddr.get(); |
| 91 | } |
| 92 | |
| 93 | bool RpcAddress::operator<(const RpcAddress& rhs) const { |
| 94 | return std::memcmp(mRawAddr.get(), rhs.mRawAddr.get(), sizeof(RpcWireAddress)) < 0; |
| 95 | } |
| 96 | |
| 97 | std::string RpcAddress::toString() const { |
Steven Moreland | 6212901 | 2021-07-29 12:14:44 -0700 | [diff] [blame] | 98 | return base::HexString(mRawAddr.get(), sizeof(RpcWireAddress)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | status_t RpcAddress::writeToParcel(Parcel* parcel) const { |
| 102 | return parcel->write(mRawAddr.get(), sizeof(RpcWireAddress)); |
| 103 | } |
| 104 | |
| 105 | status_t RpcAddress::readFromParcel(const Parcel& parcel) { |
| 106 | return parcel.read(mRawAddr.get(), sizeof(RpcWireAddress)); |
| 107 | } |
| 108 | |
| 109 | RpcAddress::~RpcAddress() {} |
| 110 | RpcAddress::RpcAddress() : mRawAddr(std::make_shared<RpcWireAddress>()) {} |
| 111 | |
| 112 | } // namespace android |