blob: ffc94b94b3f879d93262e93eae600558ce99d24d [file] [log] [blame]
Steven Moreland5553ac42020-11-11 02:14:45 +00001/*
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 Moreland62129012021-07-29 12:14:44 -070019#include <android-base/hex.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000020#include <binder/Parcel.h>
21
22#include "Debug.h"
23#include "RpcState.h"
24#include "RpcWireFormat.h"
25
26namespace android {
27
28RpcAddress RpcAddress::zero() {
29 return RpcAddress();
30}
31
32bool RpcAddress::isZero() const {
Steven Moreland91538242021-06-10 23:35:35 +000033 RpcWireAddress ZERO{.options = 0};
Steven Moreland5553ac42020-11-11 02:14:45 +000034 return memcmp(mRawAddr.get(), &ZERO, sizeof(RpcWireAddress)) == 0;
35}
36
37static 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 Moreland91538242021-06-10 23:35:35 +000055RpcAddress 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 Moreland5553ac42020-11-11 02:14:45 +000064 RpcAddress ret;
Steven Moreland91538242021-06-10 23:35:35 +000065 RpcWireAddress* raw = ret.mRawAddr.get();
66
67 raw->options = options;
68 ReadRandomBytes(raw->address, sizeof(raw->address));
69
Steven Moreland5553ac42020-11-11 02:14:45 +000070 LOG_RPC_DETAIL("Creating new address: %s", ret.toString().c_str());
71 return ret;
72}
73
Steven Moreland91538242021-06-10 23:35:35 +000074bool RpcAddress::isForServer() const {
75 return mRawAddr.get()->options & RPC_WIRE_ADDRESS_OPTION_FOR_SERVER;
76}
77
78bool 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 Moreland5553ac42020-11-11 02:14:45 +000083RpcAddress RpcAddress::fromRawEmbedded(const RpcWireAddress* raw) {
84 RpcAddress addr;
85 memcpy(addr.mRawAddr.get(), raw, sizeof(RpcWireAddress));
86 return addr;
87}
88
89const RpcWireAddress& RpcAddress::viewRawEmbedded() const {
90 return *mRawAddr.get();
91}
92
93bool RpcAddress::operator<(const RpcAddress& rhs) const {
94 return std::memcmp(mRawAddr.get(), rhs.mRawAddr.get(), sizeof(RpcWireAddress)) < 0;
95}
96
97std::string RpcAddress::toString() const {
Steven Moreland62129012021-07-29 12:14:44 -070098 return base::HexString(mRawAddr.get(), sizeof(RpcWireAddress));
Steven Moreland5553ac42020-11-11 02:14:45 +000099}
100
101status_t RpcAddress::writeToParcel(Parcel* parcel) const {
102 return parcel->write(mRawAddr.get(), sizeof(RpcWireAddress));
103}
104
105status_t RpcAddress::readFromParcel(const Parcel& parcel) {
106 return parcel.read(mRawAddr.get(), sizeof(RpcWireAddress));
107}
108
109RpcAddress::~RpcAddress() {}
110RpcAddress::RpcAddress() : mRawAddr(std::make_shared<RpcWireAddress>()) {}
111
112} // namespace android