1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_IPC_DBINDER_DATABUS_INVOKER_H 17 #define OHOS_IPC_DBINDER_DATABUS_INVOKER_H 18 19 #include <unistd.h> 20 #include <sys/types.h> 21 #include "hilog/log.h" 22 #include "dbinder_base_invoker.h" 23 #include "invoker_factory.h" 24 #include "dbinder_session_object.h" 25 #include "sys_binder.h" 26 27 #include "Session.h" 28 #include "ISessionService.h" 29 30 using Communication::SoftBus::ISessionService; 31 using Communication::SoftBus::Session; 32 33 namespace OHOS { 34 class DBinderDatabusInvoker : public DBinderBaseInvoker<DBinderSessionObject> { 35 public: 36 DBinderDatabusInvoker(); 37 ~DBinderDatabusInvoker(); 38 bool AcquireHandle(int32_t handle) override; 39 bool ReleaseHandle(int32_t handle) override; 40 void JoinThread(bool initiative) override; 41 void JoinProcessThread(bool initiative) override; 42 void StopWorkThread() override; 43 bool FlattenObject(Parcel &parcel, const IRemoteObject *object) const override; 44 sptr<IRemoteObject> UnflattenObject(Parcel &parcel) override; 45 int ReadFileDescriptor(Parcel &parcel) override; 46 bool WriteFileDescriptor(Parcel &parcel, int fd, bool takeOwnership) override; 47 pid_t GetCallerPid() const override; 48 uid_t GetCallerUid() const override; 49 uint32_t GetCallerTokenID() const override; 50 uint32_t GetFirstTokenID() const override; 51 uint32_t GetStatus() const override; 52 bool IsLocalCalling() override; 53 std::string GetLocalDeviceID() override; 54 std::string GetCallerDeviceID() const override; 55 56 bool UpdateClientSession(uint32_t handle, std::shared_ptr<DBinderSessionObject> sessionObject) override; 57 std::shared_ptr<DBinderSessionObject> QueryClientSessionObject(uint32_t databusHandle) override; 58 std::shared_ptr<DBinderSessionObject> QueryServerSessionObject(uint32_t handle) override; 59 std::shared_ptr<DBinderSessionObject> CreateServerSessionObject(binder_uintptr_t binder, uint64_t &stubIndex, 60 std::shared_ptr<DBinderSessionObject> sessionObject) override; 61 int FlushCommands(IRemoteObject *object) override; 62 63 bool OnDatabusSessionClosed(std::shared_ptr<Session> session); 64 bool OnReceiveNewConnection(std::shared_ptr<Session> session); 65 std::string ResetCallingIdentity() override; 66 bool SetCallingIdentity(std::string &identity) override; 67 int TranslateProxy(uint32_t handle, uint32_t flag) override; 68 int TranslateStub(binder_uintptr_t cookie, binder_uintptr_t ptr, uint32_t flag, int cmd) override; 69 void OnMessageAvailable(std::shared_ptr<Session> session, const char *data, ssize_t len); 70 71 private: 72 bool CreateProcessThread() override; 73 int OnSendMessage(std::shared_ptr<DBinderSessionObject> sessionOfPeer) override; 74 int OnSendRawData(std::shared_ptr<DBinderSessionObject> session, const void *data, size_t size) override; 75 std::shared_ptr<DBinderSessionObject> NewSessionOfBinderProxy(uint32_t handle, 76 std::shared_ptr<DBinderSessionObject> session) override; 77 std::shared_ptr<DBinderSessionObject> QuerySessionOfBinderProxy(uint32_t handle, 78 std::shared_ptr<DBinderSessionObject> session) override; 79 uint32_t FlattenSession(char *sessionOffset, const std::shared_ptr<DBinderSessionObject> connectSession, 80 uint64_t stubIndex) override; 81 std::shared_ptr<DBinderSessionObject> UnFlattenSession(char *sessionOffset, uint64_t &stubIndex) override; 82 uint32_t QueryHandleBySession(std::shared_ptr<DBinderSessionObject> session, uint64_t stubIndex) override; 83 uint64_t GetSeqNum() const override; 84 void SetSeqNum(uint64_t seq) override; 85 uint32_t GetClientFd() const override; 86 void SetClientFd(uint32_t fd) override; 87 void SetCallerPid(pid_t pid) override; 88 void SetCallerUid(pid_t uid) override; 89 void SetStatus(uint32_t status) override; 90 void SetCallerDeviceID(const std::string &deviceId) override; 91 void SetCallerTokenID(const uint32_t tokenId) override; 92 int CheckAndSetCallerInfo(uint32_t listenFd, uint64_t stubIndex) override; 93 uint32_t HasRawDataPackage(const char *data, ssize_t len); 94 uint32_t HasCompletePackage(const char *data, uint32_t readCursor, ssize_t len); 95 void OnRawDataAvailable(std::shared_ptr<Session> session, const char *data, uint32_t dataSize); 96 uint64_t MakeStubIndexByRemoteObject(IRemoteObject *stubObject); 97 std::shared_ptr<DBinderSessionObject> MakeDefaultServerSessionObject(); 98 bool ConnectRemoteObject2Session(IRemoteObject *stubObject, uint64_t stubIndex, 99 const std::shared_ptr<DBinderSessionObject> sessionObject); 100 bool AuthSession2Proxy(uint32_t handle, const std::shared_ptr<DBinderSessionObject> Session); 101 bool SetTokenId(const dbinder_transaction_data *tr, std::shared_ptr<DBinderSessionObject> sessionObject) override; 102 103 private: 104 DISALLOW_COPY_AND_MOVE(DBinderDatabusInvoker); 105 bool stopWorkThread_; 106 pid_t callerPid_; 107 pid_t callerUid_; 108 std::string callerDeviceID_; 109 uint32_t callerTokenID_; 110 uint32_t firstTokenID_; 111 uint64_t seqNumber_ = 0; 112 uint32_t clientFd_ = 0; 113 uint32_t status_; 114 static inline InvokerDelegator<DBinderDatabusInvoker> DBinderDatabusDelegator_ = { IRemoteObject::IF_PROT_DATABUS }; 115 }; 116 } // namespace OHOS 117 #endif // OHOS_IPC_DBINDER_DATABUS_INVOKER_H 118