• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SERVICES_DBINDER_DBINDER_SERVICE_H
17 #define OHOS_IPC_SERVICES_DBINDER_DBINDER_SERVICE_H
18 
19 #include <string>
20 #include <map>
21 #include <mutex>
22 #include <shared_mutex>
23 #include <memory>
24 #include <list>
25 #include <thread>
26 
27 #include "dbinder_service_stub.h"
28 #include "ipc_object_proxy.h"
29 #include "ipc_object_stub.h"
30 #include "rpc_system_ability_callback.h"
31 #include "Session.h"
32 
33 using Communication::SoftBus::Session;
34 
35 namespace OHOS {
36 class DBinderRemoteListener;
37 
38 constexpr int DEVICEID_LENGTH = 64;
39 constexpr int SERVICENAME_LENGTH = 200;
40 constexpr int VERSION_NUM = 1;
41 constexpr int ENCRYPT_HEAD_LEN = 28;
42 constexpr int ENCRYPT_LENGTH = 4;
43 
44 struct DeviceIdInfo {
45     uint16_t afType;
46     uint16_t reserved;
47     char fromDeviceId[DEVICEID_LENGTH + 1];
48     char toDeviceId[DEVICEID_LENGTH + 1];
49 };
50 
51 struct DHandleEntryHead {
52     uint32_t len;
53     uint32_t version;
54 };
55 
56 struct DHandleEntryTxRx {
57     struct DHandleEntryHead head;
58     uint32_t transType;
59     uint32_t dBinderCode;
60     uint32_t rpcFeatureSet;
61     uint64_t stubIndex;
62     uint32_t seqNumber;
63     binder_uintptr_t binderObject;
64     struct DeviceIdInfo deviceIdInfo;
65     binder_uintptr_t stub;
66     uint16_t serviceNameLength;
67     char serviceName[SERVICENAME_LENGTH + 1];
68     uint32_t pid;
69     uint32_t uid;
70 };
71 
72 struct SessionInfo {
73     uint32_t seqNumber;
74     uint32_t type;
75     uint32_t rpcFeatureSet;
76     uint64_t stubIndex;
77     uint32_t socketFd;
78     std::string serviceName;
79     struct DeviceIdInfo deviceIdInfo;
80 };
81 
82 enum DBinderCode {
83     MESSAGE_AS_INVOKER          = 1,
84     MESSAGE_AS_REPLY            = 2,
85     MESSAGE_AS_OBITUARY         = 3,
86     MESSAGE_AS_REMOTE_ERROR     = 4,
87 };
88 
89 enum AfType {
90     IPV4_TYPE          = 1,
91     IPV6_TYPE          = 2,
92     DATABBUS_TYPE      = 3,
93 };
94 
95 struct ThreadLockInfo {
96     std::mutex mutex;
97     std::string networkId;
98     std::condition_variable condition;
99     bool ready = false;
100 };
101 
102 class DBinderService : public virtual RefBase {
103 public:
104     DBinderService();
105     virtual ~DBinderService();
106 public:
107     static sptr<DBinderService> GetInstance();
108     bool StartDBinderService(std::shared_ptr<RpcSystemAbilityCallback> &callbackImpl);
109     sptr<DBinderServiceStub> MakeRemoteBinder(const std::u16string &serviceName,
110         const std::string &deviceID, binder_uintptr_t binderObject, uint32_t pid = 0, uint32_t uid = 0);
111     bool RegisterRemoteProxy(std::u16string serviceName, sptr<IRemoteObject> binderObject);
112     bool RegisterRemoteProxy(std::u16string serviceName, int32_t systemAbilityId);
113     bool OnRemoteMessageTask(const struct DHandleEntryTxRx *message);
114     std::shared_ptr<struct SessionInfo> QuerySessionObject(binder_uintptr_t stub);
115     bool DetachDeathRecipient(sptr<IRemoteObject> object);
116     bool AttachDeathRecipient(sptr<IRemoteObject> object, sptr<IRemoteObject::DeathRecipient> deathRecipient);
117     sptr<IRemoteObject::DeathRecipient> QueryDeathRecipient(sptr<IRemoteObject> object);
118     bool DetachCallbackProxy(sptr<IRemoteObject> object);
119     bool AttachCallbackProxy(sptr<IRemoteObject> object, DBinderServiceStub *dbStub);
120     int32_t NoticeServiceDie(const std::u16string &serviceName, const std::string &deviceID);
121     int32_t NoticeDeviceDie(const std::string &deviceID);
122     bool AttachBusNameObject(IPCObjectProxy *proxy, const std::string &name);
123     bool DetachBusNameObject(IPCObjectProxy *proxy);
124     std::string CreateDatabusName(int uid, int pid);
125     bool DetachProxyObject(binder_uintptr_t binderObject);
126     std::string QueryBusNameObject(IPCObjectProxy *proxy);
127     void LoadSystemAbilityComplete(const std::string& srcNetworkId, int32_t systemAbilityId,
128         const sptr<IRemoteObject>& remoteObject);
129     bool ProcessOnSessionClosed(std::shared_ptr<Session> session);
130     static std::string ConvertToSecureDeviceID(const std::string &deviceID);
131 
132 private:
133     static std::shared_ptr<DBinderRemoteListener> GetRemoteListener();
134     static bool StartRemoteListener();
135     static void StopRemoteListener();
136     std::u16string GetRegisterService(binder_uintptr_t binderObject);
137     bool InvokerRemoteDBinder(const sptr<DBinderServiceStub> stub, uint32_t seqNumber, uint32_t pid, uint32_t uid);
138     void OnRemoteReplyMessage(const struct DHandleEntryTxRx *replyMessage);
139     void MakeSessionByReplyMessage(const struct DHandleEntryTxRx *replyMessage);
140     bool OnRemoteInvokerMessage(const struct DHandleEntryTxRx *message);
141     void WakeupThreadByStub(uint32_t seqNumber);
142     void DetachThreadLockInfo(uint32_t seqNumber);
143     bool AttachThreadLockInfo(uint32_t seqNumber, const std::string &networkId,
144         std::shared_ptr<struct ThreadLockInfo> object);
145     std::shared_ptr<struct ThreadLockInfo> QueryThreadLockInfo(uint32_t seqNumber);
146     bool AttachProxyObject(sptr<IRemoteObject> object, binder_uintptr_t binderObject);
147     sptr<IRemoteObject> QueryProxyObject(binder_uintptr_t binderObject);
148     bool DetachSessionObject(binder_uintptr_t stub);
149     bool AttachSessionObject(std::shared_ptr<struct SessionInfo> object, binder_uintptr_t stub);
150     sptr<IRemoteObject> FindOrNewProxy(binder_uintptr_t binderObject, int32_t systemAbilityId);
151     bool SendEntryToRemote(const sptr<DBinderServiceStub> stub, uint32_t seqNumber, uint32_t pid, uint32_t uid);
152     uint16_t AllocFreeSocketPort();
153     std::string GetLocalDeviceID();
154     bool CheckBinderObject(const sptr<DBinderServiceStub> &stub, binder_uintptr_t binderObject);
155     bool HasDBinderStub(binder_uintptr_t binderObject);
156     bool IsSameStubObject(const sptr<DBinderServiceStub> &stub, const std::u16string &service,
157         const std::string &device);
158     sptr<DBinderServiceStub> FindDBinderStub(const std::u16string &service, const std::string &device);
159     bool DeleteDBinderStub(const std::u16string &service, const std::string &device);
160     sptr<DBinderServiceStub> FindOrNewDBinderStub(const std::u16string &service,
161         const std::string &device, binder_uintptr_t binderObject);
162     void ProcessCallbackProxy(sptr<DBinderServiceStub> dbStub);
163     bool NoticeCallbackProxy(sptr<DBinderServiceStub> dbStub);
164     std::list<std::u16string> FindServicesByDeviceID(const std::string &deviceID);
165     int32_t NoticeServiceDieInner(const std::u16string &serviceName, const std::string &deviceID);
166     uint32_t GetRemoteTransType();
167     bool OnRemoteInvokerDataBusMessage(IPCObjectProxy *proxy, struct DHandleEntryTxRx *replyMessage,
168         std::string &remoteDeviceId, int pid, int uid);
169     bool IsDeviceIdIllegal(const std::string &deviceID);
170     std::string GetDatabusNameByProxy(IPCObjectProxy *proxy, int32_t systemAbilityId);
171     uint32_t GetSeqNumber();
172     bool RegisterRemoteProxyInner(std::u16string serviceName, binder_uintptr_t binder);
173     bool CheckSystemAbilityId(int32_t systemAbilityId);
174     bool IsSameSession(std::shared_ptr<struct SessionInfo> oldSession, std::shared_ptr<struct SessionInfo> nowSession);
175     bool HandleInvokeListenThread(IPCObjectProxy *proxy, uint64_t stubIndex, std::string serverSessionName,
176         struct DHandleEntryTxRx *replyMessage);
177     bool ReStartRemoteListener();
178     bool ReGrantPermission(const std::string &sessionName);
179     bool IsSameLoadSaItem(const std::string& srcNetworkId, int32_t systemAbilityId,
180         std::shared_ptr<DHandleEntryTxRx> loadSaItem);
181     std::shared_ptr<struct DHandleEntryTxRx> PopLoadSaItem(const std::string& srcNetworkId, int32_t systemAbilityId);
182     void OnRemoteErrorMessage(const struct DHandleEntryTxRx *replyMessage);
183     void SendMessageToRemote(uint32_t dBinderCode, std::shared_ptr<struct DHandleEntryTxRx> replyMessage);
184 
185 private:
186     DISALLOW_COPY_AND_MOVE(DBinderService);
187     static std::mutex instanceMutex_;
188     static constexpr int WAIT_FOR_REPLY_MAX_SEC = 8;
189     static constexpr int RETRY_TIMES = 2;
190     static std::shared_ptr<DBinderRemoteListener> remoteListener_;
191     static bool mainThreadCreated_;
192     static sptr<DBinderService> instance_;
193 
194     std::shared_mutex remoteBinderMutex_;
195     std::shared_mutex busNameMutex_;
196     std::shared_mutex proxyMutex_;
197     std::shared_mutex deathRecipientMutex_;
198     std::shared_mutex sessionMutex_;
199     std::shared_mutex loadSaMutex_;
200 
201     std::mutex handleEntryMutex_;
202     std::mutex threadLockMutex_;
203     std::mutex callbackProxyMutex_;
204     std::mutex deathNotificationMutex_;
205 
206     uint32_t seqNumber_ = 0; /* indicate make remote binder message sequence number, and can be overflow */
207     std::list<sptr<DBinderServiceStub>> DBinderStubRegisted_;
208     std::map<std::u16string, binder_uintptr_t> mapRemoteBinderObjects_;
209     std::map<uint32_t, std::shared_ptr<struct ThreadLockInfo>> threadLockInfo_;
210     std::map<int, sptr<IRemoteObject>> proxyObject_;
211     std::map<binder_uintptr_t, std::shared_ptr<struct SessionInfo>> sessionObject_;
212     std::map<sptr<IRemoteObject>, DBinderServiceStub *> noticeProxy_;
213     std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> deathRecipients_;
214     std::map<IPCObjectProxy *, std::string> busNameObject_;
215     std::list<std::shared_ptr<struct DHandleEntryTxRx>> loadSaReply_;
216     static constexpr int32_t FIRST_SYS_ABILITY_ID = 0x00000001;
217     static constexpr int32_t LAST_SYS_ABILITY_ID = 0x00ffffff;
218 
219     std::shared_ptr<RpcSystemAbilityCallback> dbinderCallback_;
220 };
221 } // namespace OHOS
222 #endif // OHOS_IPC_SERVICES_DBINDER_DBINDER_SERVICE_H
223