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