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 namespace OHOS {
28 class DBinderDatabusInvoker : public DBinderBaseInvoker<DBinderSessionObject> {
29 public:
30     DBinderDatabusInvoker();
31     ~DBinderDatabusInvoker();
32     bool AcquireHandle(int32_t handle) override;
33     bool ReleaseHandle(int32_t handle) override;
34     void JoinThread(bool initiative) override;
35     void JoinProcessThread(bool initiative) override;
36     void StopWorkThread() override;
37     bool FlattenObject(Parcel &parcel, const IRemoteObject *object) const override;
38     sptr<IRemoteObject> UnflattenObject(Parcel &parcel) override;
39     int ReadFileDescriptor(Parcel &parcel) override;
40     bool WriteFileDescriptor(Parcel &parcel, int fd, bool takeOwnership) override;
41     std::string GetCallerSid() const override;
42     pid_t GetCallerPid() const override;
43     pid_t GetCallerRealPid() const override;
44     uid_t GetCallerUid() const override;
45     uint64_t GetCallerTokenID() const override;
46     uint64_t GetFirstCallerTokenID() const override;
47     uint64_t GetSelfTokenID() const override;
48     uint64_t GetSelfFirstCallerTokenID() const override;
49     uint32_t GetStatus() override;
50     virtual int32_t GetClientFd() const override;
51     bool IsLocalCalling() override;
52     std::string GetLocalDeviceID() override;
53     std::string GetCallerDeviceID() const override;
54 
55     bool UpdateClientSession(std::shared_ptr<DBinderSessionObject> sessionObject) override;
56     std::shared_ptr<DBinderSessionObject> QueryClientSessionObject(uint32_t databusHandle) override;
57     std::shared_ptr<DBinderSessionObject> QueryServerSessionObject(uint32_t handle) override;
58     std::shared_ptr<DBinderSessionObject> CreateServerSessionObject(binder_uintptr_t binder,
59         std::shared_ptr<DBinderSessionObject> sessionObject) override;
60     int FlushCommands(IRemoteObject *object) override;
61 
62     void OnDatabusSessionServerSideClosed(int32_t socketId);
63     void OnDatabusSessionClientSideClosed(int32_t socketId);
64 
65     bool OnReceiveNewConnection(int32_t socketId, int peerPid, int peerUid,
66         std::string peerName, std::string networkId);
67     std::string ResetCallingIdentity() override;
68     bool SetCallingIdentity(std::string &identity, bool flag) override;
69     int TranslateIRemoteObject(int32_t cmd, const sptr<IRemoteObject> &obj) override;
70     void OnMessageAvailable(int32_t socketId, const char *data, ssize_t len);
71 
72 private:
73     bool CreateProcessThread() override;
74     int OnSendMessage(std::shared_ptr<DBinderSessionObject> sessionOfPeer) override;
75     int SendData(std::shared_ptr<BufferObject> sessionBuff, int32_t socketId);
76     int OnSendRawData(std::shared_ptr<DBinderSessionObject> session, const void *data, size_t size) override;
77     std::shared_ptr<DBinderSessionObject> NewSessionOfBinderProxy(uint32_t handle,
78         std::shared_ptr<DBinderSessionObject> session) override;
79     std::shared_ptr<DBinderSessionObject> GetSessionForProxy(sptr<IPCObjectProxy> ipcProxy,
80         std::shared_ptr<DBinderSessionObject> session, const std::string &localDeviceID);
81     std::shared_ptr<DBinderSessionObject> QuerySessionOfBinderProxy(uint32_t handle,
82         std::shared_ptr<DBinderSessionObject> session) override;
83     uint32_t FlattenSession(char *sessionOffset, const std::shared_ptr<DBinderSessionObject> connectSession,
84         uint32_t binderVersion) override;
85     std::shared_ptr<DBinderSessionObject> UnFlattenSession(char *sessionOffset, uint32_t binderVersion) override;
86     uint32_t QueryHandleBySession(std::shared_ptr<DBinderSessionObject> session) override;
87     virtual uint64_t GetSeqNum() const override;
88     virtual void SetSeqNum(uint64_t seq) override;
89     virtual void SetClientFd(int32_t fd) override;
90     virtual void SetCallerPid(pid_t pid) override;
91     virtual void SetCallerUid(pid_t uid) override;
92     virtual void SetStatus(uint32_t status) override;
93     virtual void SetCallerDeviceID(const std::string &deviceId) override;
94     virtual void SetCallerTokenID(const uint32_t tokenId) override;
95     virtual int CheckAndSetCallerInfo(int32_t socketId, uint64_t stubIndex) override;
96     uint32_t HasRawDataPackage(const char *data, ssize_t len);
97     uint32_t HasCompletePackage(const char *data, uint32_t readCursor, ssize_t len);
98     void OnRawDataAvailable(int32_t socketId, const char *data, uint32_t dataSize);
99     uint64_t MakeStubIndexByRemoteObject(IRemoteObject *stubObject);
100     std::shared_ptr<DBinderSessionObject> MakeDefaultServerSessionObject(uint64_t stubIndex,
101         const std::shared_ptr<DBinderSessionObject> sessionObject);
102     bool ConnectRemoteObject2Session(IRemoteObject *stubObject, uint64_t stubIndex,
103         const std::shared_ptr<DBinderSessionObject> sessionObject);
104     bool AuthSession2Proxy(uint32_t handle, const std::shared_ptr<DBinderSessionObject> session);
105 
106 private:
107     DISALLOW_COPY_AND_MOVE(DBinderDatabusInvoker);
108     bool stopWorkThread_;
109     pid_t callerPid_;
110     pid_t callerUid_;
111     std::string callerDeviceID_;
112     uint64_t callerTokenID_;
113     uint64_t firstTokenID_;
114     uint64_t seqNumber_ = 0;
115     int32_t clientFd_ = 0;
116     uint32_t status_;
117     static constexpr int ACCESS_TOKEN_MAX_LEN = 10;
118     static inline InvokerDelegator<DBinderDatabusInvoker> DBinderDatabusDelegator_ = { IRemoteObject::IF_PROT_DATABUS };
119 };
120 } // namespace OHOS
121 #endif // OHOS_IPC_DBINDER_DATABUS_INVOKER_H
122