blob: 98dee9a03952d3a6729574fdc1bbb99232243dc9 [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
19#include <binder/Parcel.h>
20
21#include "Debug.h"
22#include "RpcState.h"
23#include "RpcWireFormat.h"
24
25namespace android {
26
27RpcAddress RpcAddress::zero() {
28 return RpcAddress();
29}
30
31bool RpcAddress::isZero() const {
Steven Moreland91538242021-06-10 23:35:35 +000032 RpcWireAddress ZERO{.options = 0};
Steven Moreland5553ac42020-11-11 02:14:45 +000033 return memcmp(mRawAddr.get(), &ZERO, sizeof(RpcWireAddress)) == 0;
34}
35
36static 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 Moreland91538242021-06-10 23:35:35 +000054RpcAddress 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 Moreland5553ac42020-11-11 02:14:45 +000063 RpcAddress ret;
Steven Moreland91538242021-06-10 23:35:35 +000064 RpcWireAddress* raw = ret.mRawAddr.get();
65
66 raw->options = options;
67 ReadRandomBytes(raw->address, sizeof(raw->address));
68
Steven Moreland5553ac42020-11-11 02:14:45 +000069 LOG_RPC_DETAIL("Creating new address: %s", ret.toString().c_str());
70 return ret;
71}
72
Steven Moreland91538242021-06-10 23:35:35 +000073bool RpcAddress::isForServer() const {
74 return mRawAddr.get()->options & RPC_WIRE_ADDRESS_OPTION_FOR_SERVER;
75}
76
77bool 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 Moreland5553ac42020-11-11 02:14:45 +000082RpcAddress RpcAddress::fromRawEmbedded(const RpcWireAddress* raw) {
83 RpcAddress addr;
84 memcpy(addr.mRawAddr.get(), raw, sizeof(RpcWireAddress));
85 return addr;
86}
87
88const RpcWireAddress& RpcAddress::viewRawEmbedded() const {
89 return *mRawAddr.get();
90}
91
92bool RpcAddress::operator<(const RpcAddress& rhs) const {
93 return std::memcmp(mRawAddr.get(), rhs.mRawAddr.get(), sizeof(RpcWireAddress)) < 0;
94}
95
96std::string RpcAddress::toString() const {
97 return hexString(mRawAddr.get(), sizeof(RpcWireAddress));
98}
99
100status_t RpcAddress::writeToParcel(Parcel* parcel) const {
101 return parcel->write(mRawAddr.get(), sizeof(RpcWireAddress));
102}
103
104status_t RpcAddress::readFromParcel(const Parcel& parcel) {
105 return parcel.read(mRawAddr.get(), sizeof(RpcWireAddress));
106}
107
108RpcAddress::~RpcAddress() {}
109RpcAddress::RpcAddress() : mRawAddr(std::make_shared<RpcWireAddress>()) {}
110
111} // namespace android